public
Last active

  • Download Gist
gistfile1.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 
// call when zoom level or page size changes (i.e. after zooming or after rotation)
- (void)updateContentInsetForPageScrollView:(UIScrollView *)pageScrollView {
UIImageView *imageView = (UIImageView *) [pageScrollView viewWithTag:TAG_IMAGE_VIEW];
CGFloat zoomScale = pageScrollView.zoomScale;
CGSize imageSize = imageView.bounds.size;
CGSize zoomedImageSize = CGSizeMake(imageSize.width * zoomScale, imageSize.height * zoomScale);
CGSize pageSize = pageScrollView.bounds.size;
UIEdgeInsets inset = UIEdgeInsetsZero;
if (pageSize.width > zoomedImageSize.width) {
inset.left = (pageSize.width - zoomedImageSize.width) / 2;
inset.right = -inset.left;
}
if (pageSize.height > zoomedImageSize.height) {
inset.top = (pageSize.height - zoomedImageSize.height) / 2;
inset.bottom = -inset.top;
}
pageScrollView.contentInset = inset;
}
 
-(void)scrollViewDidZoom:(UIScrollView *)pageScrollView {
[self updateContentInsetForPageScrollView:pageScrollView];
}
 
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
// loop through all pages, adjusting their sizes
// and calling updateContentInsetForPageScrollView for each
}

Thank you so much, this brought an end to 1.5 days frustration trying to get uiscrollview to work right.

Hey, watch “Session 104 - Designing Apps with Scroll Views” from WWDC 2010, they present even better way to achieve the same.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.