iOS UIScrollView with autolayout not centering correctly

*爱你&永不变心* 提交于 2019-12-25 04:17:15

问题


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:

Here is what it looks like in preview:

But here is what it looks like in the 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:

EDIT 2 Screenshot of storyboard with UILabel constraints.


回答1:


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.



来源:https://stackoverflow.com/questions/29928691/ios-uiscrollview-with-autolayout-not-centering-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!