0
votes

I am trying to wrap my head around UIScrollView with autolayout. I know this has been asked many times, but no solution has worked for me.

The view hierarchy is:

view (UIView)
  - scrollView (UIScrollView)
      - contentView (UIView)
          - label (UILabel)

The constraints are (as suggested here and everywhere else):

scrollView:

  • Leading space to container margin
  • Trailing space to container margin
  • Vertical spacing to top layout guide
  • Vertical spacing to bottom layout guide

contentView:

  • Leading space to container margin
  • Trailing space to container margin
  • Vertical spacing to container top margin
  • Vertical spacing to container bottom margin
  • Equal widths to "view"

(...that is, pinned all four sides to the scroll view and one dimension to the scroll view's superview).

This gives me the error:

Scroll View needs constraint for: height.

I further add the label inside the content view, and constrain its width, height, leading space and vertical space to container top (i.e., fixed size, and pinned to contentView's top left corner).

However, it appears in a red frame, but no missing constraints suggested in the left pane (only that for the scroll view).

I add all the missing constraints anyway: with the red octagon button in the navigation pane -for the scroll view- and the "|-triangle-|" button at the bottom right (with the label selected)

When I launch the app, my layout appears briefly and then fades to white (the view controller's view background color).

2

2 Answers

2
votes

I did the same mistake myself yesterday. You have probably put your interface in LaunchScreen.storyboard instead of Main.Storyboard or similiar. Launchscreen only provides the splash screen. Regarding your autolayout issues, I suggest using the built in "reset to suggested constraints" function.

1
votes

This is your view hierarchy

view (UIView)
  - scrollView (UIScrollView)
      - contentView (UIView)
          - label (UILabel)

All constraint added by you is okey but you have to add one more constraint on your contentView according to your scroll.

Like if you want fix height base scroll than you have to add height constraint on content view. If you want dynamic than you to add constrain equal height to scrollview and than set muliplier of equal height constraint