IOS 自动布局指南3:约束在界面生成器内是如何工作的

Working with Constraints in Interface Builder

The easiest way to add, edit, or remove constraints is to use the visual layout tools in Interface Builder. Creating a constraint is as simple as Control-dragging between two views, or to add multiple constraints at once, you simply use the various pop-up windows.


添加、编辑、移除约束最简单的方法是使用界面生成器的可视化布局工具。创建一个约束就像在两个View之间拖拽一样简单,你甚至可以一次添加多个约束,你需要做的仅仅是使用一些弹出的窗口而已。

Important: Although Xcode does not generate a warning or an error when you build a user interface that does not have appropriate constraints, you should not ship your application in such a state.

重要的是:尽管当你构建的界面没有适当的约束的时候,Xcode不会报错或者发出警告,你还是应该尽量避免你的应用进入着中国状态。


There are several ways to add constraints depending on the level of precision you want and the number of constraints you want to add at a time.


这里有几种方法添加约束,依据的是你所想要的精度标准和一次添加约束的数量。

Adding Constraints with Control-Drag(通过拖拽添加约束)

The fastest way to add a constraint is by holding down the Control key and dragging from a view on the canvas, much like the way you create links to outlets or actions. This Control-drag method is a quick, precise tool for creating a single constraint when you know exactly what type of constraint you want and where you want it.

You can Control-drag from an element to itself, to its container, or to another element. Depending on what you drag to and which direction you drag in, Auto Layout limits the possibilities of constraints appropriately. For example, if you drag horizontally to the right from an element to its container, you have the options to pin the element’s trailing space or to center it vertically in the container.


技术分享

添加一个约束最快的方法是按住control键从事图拖动到画布上,就像你创建outlet和action。当你要创建一个你知道想要类型和地方的约束的时候,这种拖动的方法是一种在当你清晰地知道你需要的那种约束,同时知道哪里找到这个约束的情况下,创建一个约束的快捷而清晰的工具的工具。

你可以从一个元素本身可是拖动,拖动到一个父容器或者另外一个元素。自动布局会根据你拖拽到的元素和你拖拽的方向来限制合适的约束。举个例子,如果你水平的从一个元素拖拽到它的容器,你会有个选项图(如上图:):the element’s trailing space(元素领先容器的空间), center it vertically in the container.(位于容器垂直方向的中心)


技术分享

Tip: To select multiple constraints at a time from the Control-drag menu, hold down the Command or Shift key.

提示:通过按住command键或者shift键,你可以一次选择多个约束

Adding Constraints with Align and Pin Menus(通过对齐和周围菜单添加约束)

You can also add constraints using the Auto Layout menu, which resides on the Interface Builder canvas.

你也可以使用自动布局菜单添加约束,自动布局菜单位于界面生成器的画布上


技术分享

In addition to adding constraints for alignment or spacing, you can also use this menu to resolve layout issues, and determine constraint resizing behavior.

技术分享
  • Align. Create alignment constraints, such as centering a view in its container, or aligning the left edges of two views.

  • Pin. Create spacing constraints, such as defining the height of a view, or specifying its horizontal distance from another view.

  • Issues. Resolve layout issues by adding or resetting constraints based on suggestions (see Resolving Auto Layout Issues).

  • Resizing. Specify how resizing affects constraints .


除了添加居中和距离约束,你也可以使用这个菜单来解决布局问题,决定约束如何改变大小的行为。
  • 对齐。创建对齐约束,比如centering a view in its container(居于容器视图中央),对齐两个视图的左边缘。
  • 定住。创建距离约束,比如定义视图的高度,或者指定这个视图和两一个视图的水平距离
  • 问题。通过根据建议添加或者设置约束来解决布局问题。(详见文章:《解决自动布局问题》)
  • 改变大小。指定何种方式约束影响视图的改变。
技术分享
Constraint options that require multiple elements are disabled if you have only a single element selected.

如果你只选择了一个元素的话多元素约束选项会被禁用。

To add a constraint from the Align or Pin menu(通过对齐或者固定菜单来添加一个约束)
  1. Select the checkbox next to the appropriate constraint.

    To select a “Spacing to nearest neighbor” constraint, select the red constraint corresponding to the appropriate side of the element.

    技术分享

    If you need to create a constraint related to another view that is not the nearest neighbor, click the black disclosure triangle in the value text field to display a drop-down menu of other nearby views.

  2. Enter a corresponding constant value.

  3. Press a button to create the constraints.

    • The Add Constraints button adds the new constraints to the selected elements.

    • The Add and Update Frames button adds the new constraints to the selected elements, and moves the elements in your interface to satisfy every constraint as well as possible.

1 选择“临近的适合约束”的复选框     
        选择一个“最近元素的距离”约束,点击合适方向的约束红线以选择。
        如果你创建的约束关系到的另一个视图不是最近的一个怎么办呢?点击黑色的三角展开按钮,会展开一个选择菜单,然后你就能选择约束的对象了
2 输入相应的值
3 点击创建约束按钮:
        “添加新约束”按钮给选择的元素添加新的约束。
        “添加和更新框架按钮”给选择的元素添加新的约束,同时按照你指定的约束移动界面元素

Note: You’re adding brand new constraints every time you click one of the two buttons. You are not editing existing constraints. For information about editing existing constraints, see Editing Constraints

注意:你每次点击以上两个按钮中的一个来添加新的约束。可是你并没有编辑已有的约束。关于编辑已有约束的信息,请看(编辑约束)。

Adding Missing or Suggested Constraints(添加缺失的的和建议的约束)

Use the Issues menu to add constraints if you need a starting point for your layout, or if you need to make a lot of changes quickly.

If you need to add a large set of constraints to describe your interface layout and you don’t want to add constraints one at a time, choose Issues > Add Missing Constraints to add a nonambiguous set of constraints. This command infers constraints based on where things are laid out.

If you need to revert to a set of constraints without errors, or you just want to start over, choose Issues > Reset to Suggested Constraints to remove erroneous constraints and add a nonambiguous set of constraints. This is equivalent to Clear Constraints followed by Add Missing Constraints.

    如果你需要对你的项目开始布局,或者需要快速布局,你可以使用问题惨淡添加约束。
    如果你需要为你的项目添加很多约束或者不愿意一次只添加一个约束,你可以这样做:选择  issues>add missing  constrants 然后添加一些确定的约束设置。这个命令依据的是对象们现在的布局。
    如果你需要重新无差错的重置约束设置,或者你仅仅是想要重新开始,选择issues> Reset to Suggested Constraints 来移除错误的约束,同事天剑正确的约束,这个操作相当于先清除约束,然后添加约束。

Editing Constraints(编辑约束)

You can change the constant, relation, and priority of a constraint. You can edit these properties either by double-clicking the constraint on the canvas and editing the value, or by selecting the constraint and using the Attributes inspector. You cannot, however, change the type of a constraint (for example, you can’t change a width constraint into a height constraint).


你可以更改约束的常数,关系,和优先级。你可以通过在画布上双击约束来修改这些属性,或者通过选择约束使用属性检查器来搞。尽管如此,你不能更改约束的种类,(举例:你不能把宽度约束改成高度约束)。

Deleting Constraints(删除约束)

Delete any constraint at any time by selecting it on the canvas or in the outline view and pressing the Delete key.

你可以在任何时候通过在画布上或者大纲视图上选择约束并且按下删除键










郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。