Anders Frohm
Anders Frohm

Reputation: 1

UIScrollView height change

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:

Thanks in advance!

Upvotes: 0

Views: 4043

Answers (2)

damian
damian

Reputation: 3674

I was having a similar problem. It turns out the issue was that I was subclassing UIScrollView for the main view of my UIViewController, and the UIViewController had its own ideas about the correct size of the scroll view.

I fixed it by making the main view subclass UIView rather than UIScrollView, then adding a UIScrollView* content_view to the view, and adding my subviews to content_view rather than to self.

Not sure if this helps you or not, however...

Upvotes: 1

Anders Frohm
Anders Frohm

Reputation: 421

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;}
}

Upvotes: 0

Related Questions