I have been struggling with this problem for some time and there seems to be no clear answer. Please let me know if anyone has figured this out.
I want to display a photo in a UIScrollView
that is centered on the screen (the image may be in portrait or landscape orientation).
I want to be able to zoom and pan the image only to the edge of the image as in the photos app.
I've tried altering the contentInset
in the viewDidEndZooming
method and that sort of does the trick, but there's several glitches.
I've also tried making the imageView
the same size as the scrollView
and centering the image there. The problem is that when you zoom, you can scroll around the entire imageView
and part of the image may scroll off the view.
I found a similar post here and gave my best workaround, but no real answer was found:
One elegant way to center the content of
UISCrollView
is this.Add one observer to the contentSize of your
UIScrollView
, so this method will be called everytime the content change...Now on your observer method:
You should change the
[pointer viewWithTag:100]
. Replace by your content viewUIView
.imageGallery
pointing to your window size.This will correct the center of the content everytime his size change.
NOTE: The only way this content don't works very well is with standard zoom functionality of the
UIScrollView
.Here is the best solution I could come up with for this problem. The trick is to constantly readjust the imageView's frame. I find this works much better than constantly adjusting the contentInsets or contentOffSets. I had to add a bit of extra code to accommodate both portrait and landscape images.
If anyone can come up with a better way to do it, I would love to hear it.
Here's the code:
Here is how I calculate the the min zoom scale in the viewDidLoad method.
This code should work on most versions of iOS (and has been tested to work on 3.1 upwards).
It's based on the Apple WWDC code for the PhotoScroll app.
Add the below to your subclass of UIScrollView, and replace tileContainerView with the view containing your image or tiles: