带有自动布局的 iOS UIScrollView 未正确居中 [英] iOS UIScrollView with autolayout not centering correctly

查看:29
本文介绍了带有自动布局的 iOS UIScrollView 未正确居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在谷歌搜索并寻找解决方案后,我决定只问一个问题.

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

我正在尝试设置一个 UIScrollView 并保留 AutoLayout.我一整天都在研究这个问题,最终通过将子视图放入其中,然后将所有子视图放入其中,使 ScrollView 与 AutoLayout 正确运行.

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.

我将 Scroll View 中的约束在所有 4 边设置为 0,与 ContentView(ScrollView 的子视图)相同,然后将 ContentView 和 ScrollView 的父视图设置为相等的宽度.

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.

然后我在 ContentView 中添加了 2 个标签,并将它们设置为水平居中,并添加了正确的约束以消除 ScrollView 的歧义.

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.

标签垂直位于正确位置,但在 iPhone 5s 上水平位于屏幕外.

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

以下是限制条件:

预览效果如下:

但这是模拟器中的样子:

But here is what it looks like in the simulator:

关于为什么会发生这种情况的任何想法?

Any ideas as to why this is happening?

提前致谢!

编辑删除等宽约束后,视图终于显示出来,但标签偏离中心:

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

编辑 2带有 UILabel 约束的故事板屏幕截图.

EDIT 2 Screenshot of storyboard with UILabel constraints.

推荐答案

好的.因此,我认为我对等宽约束的初步评论使您误入歧途 - 这是阻止内容视图扩展并带上标签所必需的.

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.

看看 this SO question,它似乎涵盖了一个类似的问题.您的约束是否与他们的匹配?

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.

这篇关于带有自动布局的 iOS UIScrollView 未正确居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆