I have a custom UIViewController subclass controlling a UISCrollView containing an UIImageView, set up like so:
UIViewController
+ UIScrollView (delegate: UIViewController, viewForZoomingInView: UIImageView)
+ UIImageView
I can use the UIScrollView to zoom in on my image, and pan around -- but if I do so such that the center of the view is not the center of the image, and then zoom out fully, my image is off center; what's more, since I am at the minimum zoom scale at that point, I cannot pan around to re-center my image.
My expectation was that when zooming out, UIScrollView would re-center the contentSize's area under its bounds so that you never see beyond the edge of the content view (or rather, the content view's size plus contentInset). Am I wrong in this assumption?
If so, what should I do to provide this behavior?
Here's 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
{
// nothing
}
EDIT: Solution!
You have to set clipsToBounds = YES
on the UIScrollView to get the desired behavior. I didn't find this initially because it is a property of UIView, not of UIScrollView directly.