为UIScrollView的儿童错误尾随空间的限制 [英] Wrong trailing space constraints for children of UIScrollView
问题描述
我创建了一个新的视图控制器,然后添加滚动视图为根视图的孩子,并设领导/顶/尾随/底部空间为上海华为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个为上海华(即滚动视图)空间限制。因此,我希望按钮,有15间隔顶部/左/右边框和滚动视图的内容在按钮+ 15的底部结束。然而,按钮的宽度是不正确的(X code 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):
如果我约束平等宽度为上海华按钮甚至不工作!仍然不占用全宽。
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?
推荐答案
找到了一个很好的解决方案:)这样做的原因行为是滚动视图调整其内容的视图大小到什么是它的子视图实际需要,并荣获 ŧ自动补的宽度/高度。如果宽度应填,滚动视图因此必须至少包含一个观点,即填满屏幕的宽度。
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.
通过这些步骤,我补充说,具有相等的宽度,以滚动视图(滚动视图本身,而不是它的内容视图区)视图,导致我的滚动视图最终扩展到全宽,因为我还添加约束空间的SuperView 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.
- 假设有一个根视图空视图控制器
- 添加滚动视图,设置限制空间,上海华顶/左/右/下= 0/0/0/0就可以了
- 添加您的UI然而滚动视图你想,也可以设置限制你想怎么
- 添加一个新的视图,它重命名为类似确保滚动视图填充宽度,有以下限制
- 前/后/顶到上海华=空间(这是滚动视图!)0
- 高度0,因为我们希望它是无形的,因为视图仅仅是一个辅助
- 重要的部分:选择此视图和层次中滚动视图,并将其约束到的宽度相等。由于滚动视图被限制在它的父,它将收到的宽度。那么这个约束确保滚动视图的内容视图还(至少)的宽。
这篇关于为UIScrollView的儿童错误尾随空间的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!