Xcode 4 和 Interface Builder:编辑垂直间距约束(锚定顶部,而不是底部)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14107799/
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-09-15 02:25:56  来源:igfitidea点击:

Xcode 4 and Interface Builder: Editing Vertical Spacing Constraint (Anchor Top, not Bottom)

iosxcodecocoa-touchinterface-builderxib

提问by jww

I've got a widget that is not laying out correctly on device (its looksOK in IB, but its not quite right). The widget is a label and its located about mid-screen. Interface Builder gave it a Vertical Space Constraint with a 'Bottom Anchor'. Here, bottom means bottom of the screen (rather than a widget below, or anchor to top screen).

我有一个小部件在设备上没有正确布局(它在 IB 中看起来不错,但不太正确)。小部件是一个标签,它位于屏幕中间。Interface Builder 给了它一个带有“底部锚点”的垂直空间约束。在这里,底部意味着屏幕的底部(而不是下面的小部件,或锚定到顶部屏幕)。

I'm in the inspector, but I don't see how to change to a top anchor (preferably, to the widget above). I tried reading Apple's docsand Editing Constraintsin particular, but it was a confusing and did not explain how to make the change (or I missed the discussion - which was 7 sentences).

我在检查员中,但我不知道如何更改为顶部锚点(最好是上面的小部件)。我尝试阅读 Apple 的文档和特别是Editing Constraints,但它令人困惑并且没有解释如何进行更改(或者我错过了讨论 - 这是 7 句话)。

Below is a screen capture under Interface Builder showing the Vertical Space Constraint anchored to the bottom of the screen. And its attributes leave a lot to be desired - Equal, Constant and Priority don't really help.

下面是 Interface Builder 下的屏幕截图,显示了锚定到屏幕底部的垂直空间约束。它的属性还有很多不足之处——平等、恒定和优先级并没有真正的帮助。

enter image description here

在此处输入图片说明

Does anyone know how to edit constraints? Specifically, I want to (1) change a vertical spacer's anchor from bottom totop; and (2) anchor against the widget above, and not the top of the screen.

有谁知道如何编辑约束?具体来说,我想(1)从下上改变垂直间隔的锚点;(2) 锚定在上面的小部件上,而不是屏幕顶部。

回答by jrturton

Select the "Embedded" and the "Calculated" element together by shift-clicking.

通过按住 Shift 键单击一起选择“Embedded”和“Calculated”元素。

With those two elements selected, use the constraints menu:

选择这两个元素后,使用约束菜单:

enter image description here

在此处输入图片说明

This menu, together with careful selection of elements, is central to happy editing of constraints in IB. In your case, choose the central item, the Pin menu. Choose Vertical Spacing - this will create a new constraint on vertical spacing between your two elements. Alternatively, select a single element and pin "Top space to superview" to pin to the top instead of the bottom.

该菜单与精心选择的元素一起,是在 IB 中愉快地编辑约束的核心。在您的情况下,选择中心项目,Pin 菜单。选择垂直间距 - 这将在两个元素之间的垂直间距上创建一个新约束。或者,选择单个元素并将“顶部空间到超级视图”固定到顶部而不是底部。

You can now select and delete the vertical spacing to the bottom of the view. IB wouldn't let you delete this before since you have to have a complete, non-ambiguous set of constraints. After adding your new vertical spacing constraint, you now have this.

您现在可以选择并删除到视图底部的垂直间距。IB 不会让您在此之前删除它,因为您必须拥有一组完整的、明确的约束。添加新的垂直间距约束后,您现在拥有了这个。

To illustrate further, here is an empty view controller, with a single text field which I have dragged on:

为了进一步说明,这里是一个空的视图控制器,我拖了一个文本字段:

enter image description here

在此处输入图片说明

All of the constraints are purple, which means IB has added them for me (they are System constraints) and they can't be removed - they are the minimum constraints needed to position and size the text field.

所有的约束都是紫色的,这意味着 IB 已经为我添加了它们(它们是系统约束)并且它们不能被删除——它们是定位和调整文本字段大小所需的最小约束。

Now, I'll select the text field, and pin the top space to the superview:

现在,我将选择文本字段,并将顶部空间固定到超级视图:

enter image description here

在此处输入图片说明

Now you can see that the two vertical space constraints have changed to blue (they are now user constraints) and they have a thicker appearance in the editor. This means that one of them can be deleted. I select the constraint for the bottom space and hit delete:

现在您可以看到两个垂直空间约束已更改为蓝色(它们现在是用户约束)并且它们在编辑器中的外观更厚。这意味着可以删除其中之一。我选择底部空间的约束并点击删除:

enter image description here

在此处输入图片说明

Note that this still has the appearance of a user constraint - but if I try to delete it, IB will automatically recreate the system constraint pinning to the bottom of the superview, getting us back to square one.

请注意,这仍然具有用户约束的外观 - 但如果我尝试删除它,IB 将自动重新创建固定到超级视图底部的系统约束,让我们回到原点。

I have written about this, and similar autolayout topics, here.

我已经写过这个和类似的自动布局主题,这里

回答by zyzzyxx

I got one word for Autolayouts. If it works, it works but if it does not, then use the old fashioned way (use code)

我对自动布局有一个词。如果有效,则有效,但如果无效,则使用老式方法(使用代码)