3

I've added a Scroll View that contains a content view. The scrollview's constraints keep it just below my progress bar and attached to the leading, trailing, and bottom of the superview. The content view's constraints hold it to the sides of the scroll view, with one additional constraint: equal the width of the superview.

Everything looks great on the storyboard preview, but at runtime the scrollview's calculated width increases.

simulator

Basically, it seems that AutoLayout is ignoring my constraint to constrain the width of the content view, and instead just allows the content view to get as large as it wants to fit the content in.

Here are my constraints:

xcode 1

Thank you so much for helping me get over this roadblock! I've been banging my head against the wall for days.

Bdennis317
  • 57
  • 8

1 Answers1

5

Here are some things I would try. I don't have comment privileges yet, otherwise I would ask for elaborations:

  1. Are there any outputs in the console for Xcode? Generally when Autolayout is forced to break constraints, it tells you about it in the logs.
  2. So to be clear, for the Content View, you have a constraint that sets the width equal to the ScrollView, as well as constraints to match the Leading and Trailing edges to the ScrollView?

If the constraint is not being followed then either the ScrollView is also expanding somehow, or the constraint conflicts with another constraint, and the width-matching constraint got broken somewhere along the way.

eager to learn
  • 388
  • 2
  • 11
  • 1) There are no outputs in the console. It's not breaking any constraints. – Bdennis317 Dec 04 '14 at 00:19
  • 4
    2) Eureka!! I did NOT have a constraint that set the content view equal to the width of the scroll view. Instead, I had a constraint that set the content view equal to the width of the main top level view (it seems like other tutorials and even some of Apple's tech notes encouraged this approach. As soon as I added the constraint you mentioned (equal widths between container view and scroll view) my problem was solved like magic. Thank you!!!! – Bdennis317 Dec 04 '14 at 00:41
  • Yay! Glad to help with minimizing your head banging :) – eager to learn Dec 04 '14 at 00:43
  • After banging my head against the wall for 2 days, I stumbled across this and it also solved my problem. However I don't understand why. Every tutorial out there says to set the equal width constraint to the top level view and it works in those demos. – Joel May 19 '16 at 14:34
  • @Joel were those tutorials working with ScrollViews as well? From my understanding, this additional constraint is needed to lock down the size of the ScrollView, which other by its very nature will expand or change size as needed. – eager to learn May 21 '16 at 21:29
  • Every tutorial and SO answer out there says to pin the content view width to the scrollview's superview. And they all work. This is very strange. – Joel May 22 '16 at 03:01