Search code examples
iosswiftautolayoutscrollview

My scrollView does not wants to scroll


This is my setup:

setup

I do not know what I am doing wrong. The image view is bigger than the size of the view and of the scroll view. The constrains are set al followed:

Scroll view: equal heights to View * 0,5, equal width to View, center Y and X to View.

View (inside Scroll view): pinned all zero's inside Scroll view, equal heights and width. I also tried instead of equal heights and widths to center X and Y inside Scroll view, but it won't scroll.

How can I let the Scroll view scroll? Thank you.


Solution

  • Add a leading, trailing and top constraint and equal height of UIScrollView to superview with 0.5 multiplier. Now to your contentView (the UIScrollView subview), add a leading, trailing , top and bottom constraint. Also add equal height and width to UIScrollView. Set the height to a priority of 250. Add constraints for UIImageView inside this contentView.

    Since the contentView will have a fixed height of low priority equal to the UIScrollView height. This fixed height constraint will break once the UIImageView total height(based on the constraints you add) will get larger than the UIScrollView height and the content will become scrollable. So at the very least you will always have a view half the screen size and become scrollable once the content becomes too large vertically.