本文翻译自:UIScrollView Scrollable Content Size Ambiguity
Fellow devs, I am having trouble with AutoLayout in Interface Builder (Xcode 5 / iOS 7). 资深开发人员,我在Interface Builder(Xcode 5 / iOS 7)中无法使用AutoLayout。 It's very basic and important so I think everyone should know how this properly works. 这是非常基本和重要的,因此我认为每个人都应该知道它是如何正常工作的。 If this is a bug in Xcode, it is a critical one! 如果这是Xcode中的错误,那就是关键!
So, whenever I have a view hierarchy such as this I run into trouble: 因此,每当我拥有这样的视图层次结构时,我都会遇到麻烦:
>UIViewController
>> UIView
>>>UIScrollView
>>>>UILabel (or any other comparable UIKit Element)
The UIScrollView has solid constraints, eg, 50 px from every side (no problem). UIScrollView具有固定的约束,例如,每侧50 px(没有问题)。 Then I add a Top Space constraint to the UILabel (no problem) (and I can even pin height / width of the label, changes nothing, but should be unneccessary due to the Label's intrinsic size) 然后,我向UILabel添加一个Top Space约束(没问题)(我什至可以固定标签的高度/宽度,什么都没有改变,但是由于Label的固有尺寸而不必要)
The trouble starts when I add a trailing constraint to the UILabel: 当我向UILabel添加尾随约束时,麻烦就开始了:
Eg, Trailing Space to: Superview Equals: 25 例如,尾随空格到:Superview等于:25
Now two warnings occur - and I don't understand why: 现在出现两个警告-我不明白为什么:
A) Scrollable Content Size Ambiguity (Scroll View has ambiguous scrollable content height/width) A)可滚动的内容大小含糊(滚动视图具有模糊的可滚动内容高度/宽度)
B) Misplaced Views (Label Expected: x= -67 Actual: x= 207 B)位置错误的视图(标签期望:x = -67实际:x = 207
I did this minimal example in a freshly new project which you can download and I attached a screenshot. 我在一个全新的项目中做了这个最小的示例,您可以下载该项目,并附上了屏幕截图。 As you can see, Interface Builder expects the Label to sit outside of the UIScrollView's boundary (the orange dashed rectangle). 如您所见,Interface Builder期望Label位于UIScrollView的边界(橙色虚线矩形)之外。 Updating the Label's frame with the Resolve Issues Tool moves it right there. 使用“解决问题”工具更新标签的框架将其移到那里。
Please note: If you replace the UIScrollView with a UIView, t