0
votes

After googling and searching for a solution to this I have decided just to ask a question.

I am trying to setup a UIScrollView with AutoLayout being left on. I have been researching this all day and finally got the ScrollView to behave correctly with AutoLayout by putting a child view inside of it and then all of the subviews inside of that.

I set up the constraints in the Scroll View to 0 on all 4 sides, same with the ContentView (subview of ScrollView) and then set the ContentView and the parent view of the ScrollView to equal widths.

I then added 2 labels into the ContentView and set them to be horizontally centered and added the correct constraints to get rid of the ScrollView ambiguity.

The labels are in the correct spots vertically, but are off screen horizontally on an iPhone 5s.

Here are the constraints:

Constraints

Here is what it looks like in preview:

Preview

But here is what it looks like in the simulator:

Simulator

Any ideas as to why this is happening?

Thanks in advance!

EDIT After removing the equal width constraints, the view is finally showing up but the labels are off center:

Simulator-Edit

EDIT 2 Screenshot of storyboard with UILabel constraints.

Simulator Labels

1
It looks to me like your content view is collapsing to zero width for some reason. As a tip, try adding a non white background colour to your views to see which one is mis-behaving. I don't think you need to set your content view to have equal width with the parent view if you have already tied its four edges to the scroll view - perhaps that is causing a conflict. Are you getting any storyboard constraint warnings?Ali Beadle
Hi Ali, thanks for the response. I did as you suggested and changed the background color of the ContentView to blue and sure enough, there was no blue. I removed the constraint for equal widths and everything seems to be working except for the label is being pushed to the right (see picture above).Brandon Shega
It seems odd that you could have had that constraint clash without any constraint warnings - are there any warnings or errors on your storyboard that you can show us?Ali Beadle
No, there are no constraint warnings and nothing in the console either =/.Brandon Shega
Possibly unrelated but - you have two labels but only one is visible on preview and simulator. Where is the other supposed to be? Is it under the first? And I can see three vertical constraints on the labels - how are they aligned vertically? Can you show us the storyboard view?Ali Beadle

1 Answers

1
votes

OK. So I think I lead you astray with my initial comment about the equal widths constraint - that is needed to stop the content view from expanding and taking your labels with it.

I am still not sure why it was collapsing to zero though.

Take a look at this SO question which seems to cover a similar issue. Do your constraints match theirs?

Edit - actually this answer may be clearer.