问题描述
我创建了一个新的视图控制器,然后添加了一个滚动视图作为根视图的子视图,并将Leading/Top/Trailing/Bottom Space to Superview"设置为 0,以便滚动视图完全填充其父视图.
I have created a fresh View Controller, then added a Scroll View as child of the root View, and set "Leading/Top/Trailing/Bottom Space to Superview" to 0 so that the scroll view would fill its parent completely.
然后我添加了一个按钮作为滚动视图的子项,空间限制为 15 到Superview"(即滚动视图).因此,我希望按钮与上/左/右边框的间距为 15,并且滚动视图的内容以按钮底部 + 15"结束.但是按钮的宽度不正确(在 Xcode 5 Interface Builder、iOS 6/7 模拟器中测试):
Then I added a button as child of the scroll view, with space constraints of 15 to "Superview" (i.e. to the scroll view). I would thus expect the button to have a spacing of 15 to top/left/right borders, and the scroll view's content to end at 'bottom of button + 15'. However the width of the button is not correct (tested within Xcode 5 Interface Builder, iOS 6/7 simulators):
如果我用Equal Width to Superview"约束按钮,它甚至不起作用!仍然没有占据整个宽度.
It doesn't even work if I constrain the button with "Equal Width to Superview"! Still doesn't take up the full width.
我该如何解决这个问题?
How do I solve this?
推荐答案
找到了一个很好的解决方案 :) 这种行为的原因是滚动视图将其内容视图大小调整为它的子视图实际需要的大小,并且不会't 自动填充宽度/高度.如果应填充宽度,则滚动视图因此必须包含至少一个填充屏幕宽度的视图.
Found a good solution :) The reason for this behavior is that the scroll view adapts its content view size to what is actually needed by its subviews, and won't fill width/height automatically. If width should be filled, the scroll view thus must contain at least one view that fills the width of the screen.
通过这些步骤,我添加了一个与滚动视图具有相同宽度的视图(滚动视图本身,而不是其内容视图区域),导致我的滚动视图最终扩展到全宽,因为我还添加了约束超级视图 0/0/0/0" 到滚动视图的空间.
With these steps, I added a view that has equal width to the scroll view (the scroll view itself, not its content view area), leading to my scroll view finally expanding to the full width because I had also added constraints "space to superview 0/0/0/0" to the scroll view.
- 假设一个带有一个根视图的空视图控制器
- 添加滚动视图,在其上设置约束superview top/left/right/bottom=0/0/0/0"
- 根据需要将 UI 添加到滚动视图,并根据需要设置约束
- 添加一个新视图,将其重命名为确保滚动视图填充宽度",具有以下约束
- 超级视图的前导/尾随/顶部空间(即滚动视图!)= 0
- 高度为 0,因为我们希望它不可见,因为视图只是一个帮助器
- 重要部分:在层次结构中选择此视图和滚动视图,并将它们限制为相等的宽度.由于滚动视图受限于其父视图,因此它将接收一个宽度.然后这个约束确保滚动视图的内容视图也(至少)那么宽.
这篇关于UIScrollView 的子项的尾随空间限制错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!