UIScrollView Zoom: Limit panning to imageView.image bounds

I have a screen-sized UIScrollView holding an identically sized UIImageView.

I have an image set inside of the UIImageView with AspectFit, so with a wider image, there are black bars on the top and bottom. This is as expected when zoomed out.

Double tapping the screen forces a [self.scrollView setZoomScale:self.scrollView.maximumZoomScale animated:YES]; The problem with this is that on images like the one mentioned above, I'm able to pan far beyond the edges of the photo.

I assume this is because I return imageView; in (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView and the imageView height is taller than the image, so I'm able to pan to the top and bottom of the imageView, ignoring its contents.

So, how do I limit panning the scrollview to the bounds of the image?


U need to use this: Emulating aspect-fit behaviour using AutoLayout constraints in Xcode 6

Aspect fit on Autolayout. Then You need add aspect ration constraint when loading image, if images are different size. U can get one error, image after zoom sticks to right edge. Still can't solve it.


Just set the content size of scrollview to the size of image just by this code:

scrollView.contentSize = [scrollView imageView].image.size;

Note that you should do this at the time you set image for your scrollview imageView.

链接地址: http://www.djcxy.com/p/68150.html

上一篇: UILabel的屏幕快照和UIImageView中的内容

下一篇: UIScrollView缩放:限制平移到imageView.image边界