1
votes

I am trying to get a simple scroll view to work with autolayout. My SuperView has a Scroll View. Inside the Scroll View is a ContentView and then a ButtonsView.

View (320x480)
  |
  |__Scroll View(320x480)
          |
          |__ContentView(320x556)
                 |
                 |__ButtonsView(320x175)

The ScrollView constraints to the SuperView are 0,0,0,0 for Leading, Trailing, Bottom and Top. The ContentView constraints to the Scroll View are 0,0,-96,0 for Leading, Trailing, Bottom and Top. The -96 is because my content view height is 556. I have a ButtonsView inside the ContentView. The ButtonView constraints to the ContentView are 0,0,238,64.35 for Leading, Trailing, Top and Aspect Ratio.

I am getting Missing Constraints and Scrollable Content Size Ambiguity errors on my xib.

I do not want to to hardcode the width and height for either my ContentView or ButtonsView as I want the xib to work for iPad as well.

Can someone please tell me what I am doing wrong and how I can get this fixed?

enter image description here

enter image description here

Thanks

This is not a duplicate question. The question above has a scrollview which doesnt work as intended. My question is more about how autolayout rules can be applied to the scrollviews.

2

2 Answers

3
votes

You should let the inner contents of your ScrollView to determine its content size. Whether by indicating fixed height and width (which you don't prefer). Or by putting constraints to determine the sizes of inner contents related to the View. So in your specific case, you can add an Equal Widths Constraint for ContentView and View. To adjust height, you can add an Equal Heights Constraint for ButtonsView and ContentView with multiplier of 175/556

1
votes

You just need to add a Bottom constraint in your ButtonsView to the ContentView. With those constraints, now the ContentView can guess its height, and so the ScrollView, too.