问题描述
我正在 Xcode 5 中构建一个 iOS 应用程序,但在添加布局约束时遇到了问题.在 Xcode 4.x 中,我可以将前导/尾随/顶部/底部空格添加到容器(超级视图)到任何 UI 元素.现在,我只有这个选项:
I am building an iOS app in Xcode 5, and I'm having trouble adding layout constraints. In Xcode 4.x, I could just add leading/trailing/top/bottom spaces to container (superview) to any UI element. Now, I only have this option:
我想在我的文本视图中添加一个超级视图的底部空间"约束.它说与最近邻居的间距",但我不想创建对最近邻居"的约束,我的文本视图下还有其他东西,我不想用它来创建约束.如果我的文本视图下没有其他对象,它会正确地向容器添加一个约束.但是,如果我尝试更改任何内容,一切都会搞砸:我需要将所有视图移到其他位置,然后添加约束,然后添加将其他视图移回.如果我在文本视图和 UI 元素(在这种情况下是一个按钮)之间添加一个约束,它就不起作用(一切似乎都正确:按钮对超级视图的底部有一个约束,而文本视图有一个垂直间距约束)按钮,并且没有冲突的约束).UI 元素以不同的(3.5 英寸)屏幕方向显示在屏幕边界之外.Interface Builder 是否存在错误,或者我遗漏了什么?当我可以正确设置所有内容时,当我调整界面构建器屏幕的大小时,我的控件似乎仍然超出范围,但在 3.5 英寸的模拟器/设备上正确显示.在 Xcode 5 中使用新的 Interface Builder 约束来设计 UI 的最佳实践"是什么?
I want to add a "bottom space to superview" constraint to my text view. It says "spacing to nearest neighbor", but I don't want to create a constraint to "nearest neighbor", I have something else under my text view, and I don't want to create a constraint with that. If there is no other object under my text view, it adds a constraint to the container correctly. But if I try to change anything, everything messes up: I need to move all the views somewhere else, then add the constraint, then add move the other views back. If I add a constraint between the text view and the UI element (a button, in this case), it doesn't work (everything seems correctly: button has a constraint to superview's bottom, and text view has a vertical spacing constraint with the button, and there are no conflicting constraints). The UI elements appear out of screen bounds in different (3.5 inch) screen orientations. Is there a bug with the Interface Builder, or am I missing something? When I could set everything up correctly, my controls still seem out of bounds when I resize the interface builder screens, but display correctly on simulator/device at 3.5 inches. What is the "best practice" of designing the UI with the new Interface Builder constraints in Xcode 5?
更新:我在另一个项目中遇到了更多的问题.我试图通过 CTRL 将我的控件拖动到它说顶部布局指南"的位置来精确地修复视图的顶部到顶部布局指南,并且它正试图将我的控件底部与布局指南相匹配(大约 -470点)而不是调整我的控件的顶部,这没有任何意义,因为控件应该在可变高度的屏幕中进行布局(例如常规和 4 英寸 iPhone,以及将来可能的其他布局..).这是正在发生的事情的屏幕截图:
UPDATE: I'm having some more trouble in another project with the same thing. I am trying to fix a view's top to the top layout guide exactly by CTRL-dragging my control to where it says "top layout guide", and it is trying to aling the bottom of my control to the layout guide (by about -470 points) instead of alinging the top of my control, which doesn't make any sense as the control should layout itself in variable height screens (e.g. regular and 4-inch iPhone, and the possible other layouts in the future..). Here is a screenshot of what's happening:
我在约束中得到了这个:
And I'm getting this in constraints:
如何让 Interface Builder 将视图的 top 与顶部布局指南对齐?
How can I make Interface Builder align the top of the view to the top layout guide?
更新 2:我找到了解决方法.我首先调整视图的大小以减小它的高度(因此它的顶部不会重叠或非常靠近顶部布局指南),然后我 CTRL+将其拖动到顶部布局指南.它似乎工作正常.连接后,我再次将视图调整为所需大小并选择Update Constraints
.它可以工作,但仍然不是解决方案,因为它需要调整视图大小、连接、再次调整大小和更新约束.
UPDATE 2: I've found a workaround. I first resize the view to decrease it's height (so the top of it will not overlap or be very close to the top layout guide) and THEN I CTRL+drag it to the top layout guide. It seems to work correctly. After I connect it, I resize my view again to the desired size and select Update Constraints
. It works, but it's still not a solution as it requires resizing the view, connecting, resizing again, and updating constraints.
推荐答案
要在视图和它的父视图之间添加约束(例如底部空间到父视图"),您可以使用文档大纲(界面的左侧生成器).
To add a constraint between a view and its' superview (like "bottom space to superview") you can use the Document Outline (The left part of the Interface Builder).
在文档大纲中:
- 按住 ctrl 键并从视图拖动到其父视图(反之亦然).
- 选择您要添加的约束(在本例中为底部空间到容器").
这篇关于在 Xcode 5 中为超级视图添加间距约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!