I am making a simple app for the iPad that consists of a giant image that you can zoom-in/out on and navigate.
This image is placed inside a
// Set the needed attributes of the imageView
[imageView setImage:[UIImage imageNamed: @"TestImage.png"]];
imageView.userInteractionEnabled = YES;
// Set a bunch of the imageScrollView attributes.
// calculate minimum scale to perfectly fit image width, and begin at that scale
float minimumScale = [imageScrollView frame].size.width / [imageView frame].size.width;
[imageScrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
imageScrollView.clipsToBounds = YES;
imageScrollView.bounces = FALSE;
imageScrollView.bouncesZoom = FALSE;
I fixed it!
For anyone else having the same problem, here's how I fixed the code:
The Cause of the bug was that Interface Builder was forcing the views to use the
contentMode. Setting the
ScrollView's contentMode to
UIViewContentModeScaleAspectFit didn't work on its own, since the
imageView was still being distorted.
The following code had to be added at the end of the setup for the
UIImageView (inside of the
[imageScrollView setContentMode:UIViewContentModeScaleAspectFit]; [imageView sizeToFit]; [imageScrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
contentMode is set to
AspectFit, like @Mike suggested. Then, sizeToFit() is called on the
imageView to override Interface Builder's annoying presets, and to make the image the correct size.
The last line is important. You must set the
contentSize for the ScrollView AFTER you re-size the
imageView. Otherwise, the
ScrollView will think that the image is still the size of the iPad display, and therefore won't think that there's anything to scroll. Setting the size of the
ScrollView after resizing the
ImageView and changing the
contentMode makes the
ScrollView the actual size of the image.
Hope that helped!
Thanks for the help Mike, it got me on the right track :)