Search code examples
iosautolayoutscrollvieworientation

ScrollView isn't resizing properly


I have a ScrollView with vertical scrolling. The reason for the ScrollView is to fit all of my buttons when the orientation changes to landscape.

There are 2 problems that are possibly linked:

  • When in portrait (with no need for scrolling) the view still wants to scroll.
  • When in landscape, it leaves too much scrolling space at the bottom.

Constraints:

  • ScrollView - Trailing/Leading space to superView, Bottom space to label2, Top space to label1
  • ContentView - Trailing/Leading/Bottom/Top space to superView, Width equal to mainView
  • Label1 - Align Center X, Top space to superView (60), Bottom space to scrollView
  • Label2 - Align Center X, Bottom space to superView (60), Top space to scrollView
  • Buttons - Align Center X, Top space to superView (50), Height equals (50), Bottom space to other button (30) or Bottom space to superView (50)

Adjust Scroll View Insets is unchecked. Its all in autolayout, no code.

View Controller Example:

enter image description here

What's strange, is that I have other perfectly working scroll views that resize accordingly. When trying to duplicate the process, I kept getting the same results...

Looked around, but haven't been able to find any helpful answers. So any help would be greatly appreciated.

I'll add other information if needed. I'm sure its a simple solution that I keep overlooking -_-


Solution

  • Anchor scroll view leading and width to view's leading and width; anchor top to label1 bottom and bottom to label2 top.

    Get rid of the content view (not needed). Add everything to the scroll view.

    Anchor button1 center-x to scroll view center-x and top to scroll view top. If you need to give button1 width or height using constraints, do not use the scroll view's width or height; instead, anchor the button's width to the view's width, height to view's height. Subviews of scroll view should not rely on the scroll view to determine their sizes.

    Anchor button4 top to button3 bottom, center-x to scroll view center-x; and most importantly, anchor button4 bottom to scroll view bottom (this will stretch the scroll view for you). This will prevent scrolling when scrolling isn't needed.