ios 在 Xcode 5 中为 superview 添加间距约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18959118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:34:23  来源:igfitidea点击:

Add spacing constraint to superview in Xcode 5

iosxcodeinterface-builderautolayoutxcode5

提问by Can Poyrazo?lu

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:

我正在 Xcode 5 中构建 iOS 应用程序,但在添加布局约束时遇到问题。在 Xcode 4.x 中,我可以将前导/尾随/顶部/底部空格添加到容器(超级视图)到任何 UI 元素。现在,我只有这个选项:

enter image description here

在此处输入图片说明

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?

我想在我的文本视图中添加一个“底部空间到超级视图”约束。它说“与最近邻居的间距”,但我不想为“最近邻居”创建约束,我的文本视图下还有其他内容,我不想用它创建约束。如果我的文本视图下没有其他对象,它会正确地向容器添加约束。但是如果我试图改变任何东西,一切都会变得一团糟:我需要将所有视图移动到其他地方,然后添加约束,然后添加将其他视图移回。如果我在文本视图和 UI 元素(在本例中为按钮)之间添加约束,则它不起作用(一切似乎都正确:按钮对超级视图底部有约束,文本视图有垂直间距约束按钮,并且没有冲突的约束)。UI 元素出现在不同(3.5 英寸)屏幕方向的屏幕边界之外。Interface Builder 是否存在错误,还是我遗漏了什么?当我可以正确设置所有内容时,当我调整界面构建器屏幕大小时,我的控件似乎仍然超出范围,但在模拟器/设备上以 3.5 英寸正确显示。在 Xcode 5 中使用新的 Interface Builder 约束设计 UI 的“最佳实践”是什么?

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:

更新:我在另一个项目中遇到了同样的问题。我正在尝试通过 CTRL 将我的控件拖动到显示“顶部布局指南”的位置,将视图的顶部完全固定到顶部布局指南,并且它试图将我的控件的底部对齐到布局指南(大约 -470点)而不是对齐我的控件的顶部,这没有任何意义,因为控件应该在可变高度的屏幕(例如普通和 4 英寸 iPhone,以及未来可能的其他布局......)中自行布局。这是正在发生的事情的屏幕截图:

enter image description here

在此处输入图片说明

And I'm getting this in constraints:

我在限制条件下得到了这个:

enter image description here

在此处输入图片说明

How can I make Interface Builder align the topof the view to the top layout guide?

我怎样才能让Interface Builder的对齐的视图顶端布局指南?

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.

更新 2:我找到了一种解决方法。我首先调整视图的大小以降低它的高度(因此它的顶部不会重叠或非常接近顶部布局指南),然后我按 CTRL + 将其拖动到顶部布局指南。它似乎工作正常。连接后,我再次将视图调整为所需大小并选择Update Constraints。它有效,但它仍然不是解决方案,因为它需要调整视图大小、连接、再次调整大小和更新约束。

回答by Jarig

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).

要在视图与其超级视图之间添加约束(例如“底部空间到超级视图”),您可以使用文档大纲(界面生成器的左侧部分)。

In the Document Outline:

在文档大纲中:

  1. Hold the ctrl-key and drag from the view to its' superview (or vice versa).
  2. Select the constraints you want to add (in this example "Bottom Space to Container").
  1. 按住 ctrl 键并从视图拖动到其超级视图(反之亦然)。
  2. 选择要添加的约束(在本例中为“底部空间到容器”)。

ctrl-drag from the view to its' superview (or vice versa)Select the constraints you want to add

ctrl-drag from the view to its' superview (or vice versa)Select the constraints you want to add

回答by kball

The answer By Jarig is good but can sometimes lead to unexpected results as noted in the comments.

Jarig 的回答很好,但有时会导致如评论中所述的意外结果。

Another option is to select the child view and go to:

另一种选择是选择子视图并转到:

Editor > Pin > Top Space to Superview

编辑器 > 固定 > 顶部空间到超级视图

or whichever constraint you want.

或您想要的任何约束。

回答by epolyakov

Seems to be a bottom edge constraint now. Select your view and its superview, tap on "Align" and check the "Bottom edges" thingy

现在似乎是底部边缘约束。选择您的视图及其超级视图,点击“对齐”并检查“底部边缘”

pic

pic

回答by AlexR

If you want to add trailing or leading constraints as in Xcode 4 please CTRL-drag from the center of the view to which you want to add the constraint to the left, right, up or down. You will then be presented a window in which you can select the constraint to add (depending on the direction in which you CTRL-dragged).

如果您想像在 Xcode 4 中一样添加尾随或前导约束,请按住 CTRL 键并从要添加约束的视图中心向左、向右、向上或向下拖动。然后您将看到一个窗口,您可以在其中选择要添加的约束(取决于您按住 CTRL 键拖动的方向)。