How to setup UIScrollView and UIImageView

Hi, I have an UIScrollView and under it a UIImageView as subview. The UIScorllView size is 375x375 and UIImageView size is also 375x375 in mainStoryBoard. The UIImageView uses "Aspect Fill". I want the user be able to pan and scrool the UIImageView to see any part of the image in any zoom scale. For example, when I set an rectangular image 1024 x 1780 to the image view. the user shoule be able to move the image up and down to see the top and bottom portion. I found however, in my setup, the user can zoom, but they won't able pan to see the top and bottom portion of the image. It feel likes the UIImageView clip the original into sqaure and won't let use to see any portion out of this square.


How can I fix it?


Thanks

It has to do with the constraints you've set up on the scrollview and its contentView. See for instance:


https://developer.apple.com/library/content/technotes/tn2154/_index.html


One thing that's worked for me is to set the width/height constraints of the contentView to be >= the size of the scrollView.

I have no idea what the document is talking about... The worst document I have ever seen.

It (that is, your app) may be doing what you told it to do. If you created a UIImageView with a fixed size of 375 x 375 and Aspect Fill mode, then any image you load into the view is going to be scaled to 375 pts wide or high, and there will be no scrolling. If you then zoom the image, the image view will stay at its fixed size, and you won't be able to scroll (because scrolling works by repositioning the image view in the scroll view, not by repositioning the image in the image view).


So, it sounds like you need to unconstrain the width and height of the image view in IB. How you would go about that (or even whether that's the right answer) depends on details of your storyboard that we don't have.


It may also be — I can't remember offhand — that your scenario is a bit more difficult that it seems because you are apparently relying on two kinds of automatic scaling at the same time: scaling of an arbitrary image to fit in a fixed rectange (375 wide or high), followed by scaling to zoom the image. You might have to work harder to get this to zoom properly via the scroll view.

How to setup UIScrollView and UIImageView
 
 
Q