iosswiftuiimageviewscrollviewcenter

UIImageView inside Scrollview - out of center while zooming


I am trying to place an UIImageView inside a Scrollview. I made constraints to center the UIImageView inside the Scrollview. That works, but if I zoom in, the position shifts to the right. So e.g. I am not able to zoom into the Image and see the far right part of the image.

It looks like that: enter image description here

Any ideas why this happens, and how I can solve the problem?

Thank you very much!

EDIT:

Behaviour at the moment: enter image description here

How it should behave: enter image description here


Solution

  • Positioning or constraining something directly inside a scroll view makes absolutely no sense at all (unless you know how to leverage derived height of childs to work for you correctly)

    A UIScrollView is not a UIView per se. It providers a wrapper around child views that are scrollable within a fixed height(the height of the UIScrollView).

    The content that will scroll will be the height of the contentView (direct child of the UIScrollView or the derived height of all items inside). Note I'm talking about vertical scroll view here. Horizontal has the same principles.

    If you want to center the UIImageView inside the "content" of the UIScrollView, then define one child UIView under the UIScrollView and give it a height larger than the UIScrollView height. You can then try centering your UIImageView by setting constraints to the UIView.

    -> UIScrollView
        -> ContentView (UIView) //constraint this and pin edges to UIScrollView
            -> UIImageView //constraint this with the ContentView, not UIScrollView