Skip to content

Instantly share code, notes, and snippets.

@andreyvit
Created April 29, 2010 22:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andreyvit/384389 to your computer and use it in GitHub Desktop.
Save andreyvit/384389 to your computer and use it in GitHub Desktop.
// 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
}
@sdsykes
Copy link

sdsykes commented Sep 9, 2010

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

@andreyvit
Copy link
Author

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

@NiklasOemler
Copy link

10 year old code but saved me so many nerves! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment