I have been unable to find the answer for this issue I am having.
I have a UIImageView inside a UIScrollView, and I would like the center its content vertically.
Right now, the only way I have been able to do this is by setting the scroll view's contentInset based on the height of the UIImageView size, but this is not a perfect solution; it just increases the size of the UIImageView, making the UIScrollView 'think' its content is bigger, and adds these black bars to the top.
I've tried to get help from:
UIScrollView with centered UIImageView, like Photos app
and
Center content of UIScrollView when smaller
But have not been able to solve it using those answers.
Best Answer
This code should work on most versions of iOS (and has been tested to work on 3.1 upwards, which is all I currently have access to).
It's based on the Apple WWDC code mentioned in Jonah's answer.
Add the below to your subclass of UIScrollView, and replace tileContainerView with the view containing your image or tiles: