views:

153

answers:

1

Hi!

I have a UITableView containing a list of maps. When an item is selected a new sub view is added to the main window and the map is shown. In my view controller for the map view the following method gets called each time it's displayed:

- (void)showMap {
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:mapImage]];

float imageHeight = tempImageView.image.size.height;
float targetHeight = scrollView.frame.size.height - mapNavigationController.navigationBar.frame.size.height;

scrollView.contentSize = tempImageView.image.size;
scrollView.maximumZoomScale = 1.0;
scrollView.minimumZoomScale = targetHeight / imageHeight;
scrollView.clipsToBounds = YES;

self.imageView = tempImageView;
[tempImageView release];

[scrollView addSubview:imageView];
scrollView.zoomScale = scrollView.minimumZoomScale;}

A new UIImageView is added each time to the UIScrollView and when the view disappears the UIImageView is removed. The zoomScale is calculated so that the image fits according to the aspect ratio.

Now to my problem the first time an item is clicked everything works fine and the map image fills the full height of the UIScrollView, but the next time an item is clicked the height of the UIScrollView is smaller. I also should note that the main window contains a tab bar.

So now to my questions:

  • How can the UIScrollView change it's size?
  • And is there any other dynamic way to retrieve the targetHeight?

Thanks in advance!

A: 

Ok, i managed to found a solution my self, maybe not the best but it's dynamic and it works.

But i still don't understand why the height of UIScrollView would change. If someone know why please comment.

NSInteger scrollViewHeight = 0;

- (void)showMap {
    if (scrollViewHeight == 0) {
        scrollViewHeight = scrollView.frame.size.height;
    } else {
        scrollView.frame.size.height = scrollViewHeight;
    }

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:mapImage]];

    float imageWidth = tempImageView.image.size.width;
    float imageHeight = tempImageView.image.size.height;
    float targetWidth = scrollView.frame.size.width;
    float targetHeight = scrollViewHeight - mapNavigationController.navigationBar.frame.size.height;
    float scale = targetHeight / imageHeight;

    if (scale * imageWidth < targetWidth) {
        scale = targetWidth / imageWidth;
    }

    scrollView.contentSize = tempImageView.image.size;
    scrollView.maximumZoomScale = 1.0;
    scrollView.minimumZoomScale = scale;
    scrollView.clipsToBounds = YES;

    self.imageView = tempImageView;
    [tempImageView release];

    [scrollView addSubview:imageView];
    scrollView.zoomScale = scale;}
}
Anders Frohm