I have a custom subclass of UIViewController managing a UISCrollView containing a UIImageView configured like this:
UIViewController + UIScrollView (delegate: UIViewController, viewForZoomingInView: UIImageView) + UIImageView
I can use UIScrollView for zooming and panning - but if I make the center of the view not be the center of the image and then completely reduce the image, my image is off center; which is more, since I am in the minimum scale at this moment, I cannot move to re-center my image.
I expected that when scaling, the UIScrollView would reorient the contentSize area beneath its borders so that you would never see outside the content view (more precisely, the size of the content view plus the contentInset). Am I mistaken in this assumption?
If so, what should I do to ensure this behavior?
Here is my code:
- (void)viewDidLoad { [super viewDidLoad]; imageView.image = image; [imageView sizeToFit]; float minimumScale = [imageScrollView frame].size.width / [imageView frame].size.width; imageScrollView.contentSize = [imageView frame].size; imageScrollView.maximumZoomScale = 2.0; imageScrollView.minimumZoomScale = minimumScale; imageScrollView.zoomScale = imageScrollView.minimumZoomScale; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return imageView; } - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
EDIT: solution!
You must set clipsToBounds = YES to UIScrollView to get the desired behavior. I did not find this initially because it is a UIView property, not a UIScrollView directly.
source share