的开发者, 我在界面生成器(Xcode 5 / iOS 7)中的自动布局遇到了麻烦。 这是非常基础和重要的,所以我认为每个人都应该知道这是如何正确工作的。如果这是Xcode中的一个bug,那它就是一个严重的bug !

所以,每当我有这样的视图层次结构时,我就会遇到麻烦:

>UIViewController
>> UIView
>>>UIScrollView
>>>>UILabel (or any other comparable UIKit Element)

UIScrollView有固定的约束,例如,每边50px(没问题)。 然后我添加一个顶部空间约束UILabel(没有问题)(我甚至可以钉标签的高度/宽度,改变什么,但应该是不必要的,由于标签的内在大小)

当我给UILabel添加一个尾随约束时,问题就开始了:

例如,尾随空间到:Superview等于:25

现在出现了两个警告——我不明白为什么:

A)滚动内容大小不明确(滚动视图具有不明确的滚动内容高度/宽度)

B)视图错位(Label Expected: x= -67 Actual: x= 207)

我在一个新项目中做了这个最小的例子,你可以下载,我附上了一张截图。如你所见,Interface Builder期望Label位于UIScrollView的边界之外(橙色虚线矩形)。用解决问题工具更新标签的框架,将其移到那里。

请注意:如果你用一个UIView替换UIScrollView,行为是预期的(标签的帧是正确的,根据约束)。所以看起来要么是UIScrollView有问题,要么是我错过了一些重要的东西。

当我运行应用程序而不更新标签的框架时,它的位置很好,正是它应该在的地方,UIScrollView是可滚动的。 如果我确实更新了帧,标签就会从视野中消失,UIScrollView也不会滚动。

帮帮我,欧比王·克诺比!为什么是模棱两可的布局?为什么会出现这种错位的观点?

你可以在这里下载示例项目,试着看看你能弄清楚发生了什么: https://github.com/Wirsing84/AutoLayoutProblem


当前回答

Xcode 11 +。

使用自动布局的最简单方法:

Add UIScrollView and pin it 0,0,0,0 to superview (or your desired size) Add container of UIView type inside ScrollView, pin it 0,0,0,0 to all 4 sides and center it horizontally and vertically. In size inspector of container, change bottom and align center Y priority to 250. (for horizontal scroll change trailing and align center X) Add all views that you need into said container (UIView). Don't forget to set the bottom constraint on the lowest view. Select the UIScrollView, select the size inspector and deselect Content Layout Guides.

其他回答

在我的情况下,我收到了不正确的内容大小和内容大小模糊的问题在iPad,但在iPhone的情况下工作。我在故事板中做了以下更改来解决这个问题。

在UIView中添加scrollview并添加约束前置,上,后,下到0,0,0,0。 设置滚动视图的高度根据要求,例如。One hundred. 添加UIView到滚动视图,并添加约束领先,顶部,尾部和底部0,0,0,0和对齐中心(X)和中心(Y)约束。 在滚动视图的尺寸检查器中取消选择“内容布局指南”。

我得到了同样的错误。我已经做了以下工作

视图(父视图) 滚动视图0,0600600 ScrollView中的UIView: 0,0,600,600 UIView包含图像视图,标签

现在为scrollView(2)和UIView(3)添加开头/结尾/顶部/底部。

选择视图(1)和视图(3)设置相同的高度和重量。它解决了我的问题。

我已经做了一个视频,这将会有所帮助:

https://www.youtube.com/watch?v=s-CPN3xZS1A

Xcode 11+, Swift 5

如果你想知道为什么所有的答案都不再工作了,请确保你已经固定了内容视图(你已经放在滚动视图内的那个)到滚动视图的内容布局指南,而不是框架布局指南。

内容视图边缘(前导,后拉,顶部,底部)不应该像图像-框架布局指南上的前导那样被固定

但像这样-内容布局指南。

然后大部分答案都会适用于你。

现在最简单的方法是这样的:

把滚动视图放到根视图中 将滚动视图的所有边固定在父视图上 取另一个UIView(我们称之为content view)把它放到滚动视图中 将内容视图的所有边固定在滚动视图的内容布局指南上 固定内容视图的宽度等于滚动视图的框架布局指南 以任何你想要的方式固定内容视图的高度(我在下面的例子中使用了常量高度约束)

总的来说,在最简单的实现中,你的结构是这样的

如果你仍然有UIScrollView的问题,只要关闭内容布局指南 (在xcode Interface Builder中选择你的ScrollView ->在右侧面板中选择尺寸检查器->取消选择“内容布局指南”) 或者试试这些步骤:xcode 11滚动视图布局-它可以 对新的版式很有用。适用于macOS 10.15.2, Xcode 11.3.1, 05.02.2020

看到截图

对于任何视图,框架及其内容的大小是相同的,即如果你有一些内容(例如图像)的大小为200 * 800,那么它的框架也是200 * 800。

这对于scrollview的内容是不正确的。内容通常大于scrollView的帧大小。如果内容宽度相同,则只能垂直滚动。如果高度相同,则只能水平滚动。因此它是唯一需要6个约束而不是4个约束的视图。对于任何其他具有超过4个必要约束的视图都将导致冲突。


要设置你的滚动视图,它的内容和滚动的位置,你基本上需要回答三个问题:

我的身材有多大?即在任何给定时刻滚动视图应该有多大?2 .拓展例句我只想用屏幕一半的高度,所以

scrollview.frame = (x: 0, y:0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height / 2)

How much scrolling space do I need? ie How big is the content? e.g. the frame can be just 500 points of width, but then you can set an image with 7000 points, which would take quite some time to scroll horizontally. Or let it be exactly 500 points of width which then means no horizontal scrolling can happen. How much have you scrolled at the moment? Say your content's (or image) width was 7000 and the frame size is just 500. To get to the end of the image, you'd need to scroll 6500 points to the right. The 3rd part really doesn't affect the constraints. You can ignore that for now. Understanding it just helps how a scrollView works.

解决方案

基本上,如果你忽略了2个额外的约束(关于内容大小),那么布局引擎将会因为模糊性而抱怨。它不知道内容的哪些区域被隐藏(在scrollView中不可见),哪些区域的内容不被隐藏(在scrollView中可见)。

所以一定要为内容添加大小限制。要了解更多,请看这个答案

但有时我不添加大小限制到我的视图,它只是工作。为什么呢?

If all the contents you've added into the scrollview are constrained to the edges of the scrollview, then as the content grow the scrollview will add space to accommodate. It's just that you might be using UIViews where its intrinsicContentSize is 0 so the scrollView will still complain about ambiguity of its content. However if you've used a UILabel and it has a non-empty text, then its intrinsicContentSize is set (based on Font size and text length and line breaks, etc) so the scrollView won't complain about its ambiguity.