Search code examples
iosuiscrollviewzoomingalignmentcenter

Using UIScrollView, when Zooming Out, how can I keep the content view at the center?


The minimum scale is 0.5

The maximum scale is 2.0

I need to always use larger contentSize for UIScrollView to be able to drag around the content. So, as the CGRect of the content is changing, contentSize of UIScrollView is also changing.

However, as I zoom out, if the scale is going below 1.0, contentOffset is abruptly changed to {0.0, 0.0}, making it very difficult to resize the content while keep it's position to the overlay view (e.g. a clothe image).

In other words, I can't make the content view smaller while keeping it positioned at the center of the UIScrollView.

Please help me to find the right way to use zoom in/out with UIScrollView while keeping the position of the content view.


Solution

  • The solutions is basically about using contentInset at initial setup to keep the content at the center, instead of contentOffset. With contentInset being unmodified, zoomed in or out object can be at the center all the time.