This is part of “Make my first iPhone app in 16 weeks” series.
I have been working on the assignment 4. I carefully re-watched the video #9 to learn more about Table View and Scroll View.
If you were to have a UIView which contains UIScrollView into which you programmatically input an UIImageView, you have to do several things to display the image in the way you want.
First of all, you must set constraints on UIScrollView relative to its super view – UIView. This keeps the scroll view to have consistent layout when the device orientation changes or the device itself changes from iPhone 3 to iPhone 4 and up. For my purpose, I made sure leading, trailing, top and bottom distance are zero, so that it takes up the full screen regardless of the device or orientation.
Second, you must set content size on the scroll view to be the same as the size of image.
self.scrollView.contentSize = image.size;
Third, you should set the frame of the image which determines where the images will be located on the Scroll View content area. Below code will fill up the entire scroll view with the image.
self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
Fourth, if you want your scroll view to zoom into a specific part of the image (i.e. 300 x 200 pt), use zoomToRect method on the scroll view.
[self.imageScrollView zoomToRect:CGRectMake(0, 0, 300, 200) animated:YES];
Fifth, other properties of scroll view to be aware of. Content offset gives you where your upper left corner of the bounds of your scroll view is in its content area. Below code returns a CGPoint which has x (horizontal offset in the coordinates) and y (vertical offset in the coordinates).
Below code returns the CGRect (width and height of scroll view’s bounds).