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 theAuto 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 (seeResolving Auto Layout Issues).

Resizing.Specify how resizing affects constraints .

除了添加居中和距离约束,你也可以使用这个菜单来解决布局问题,决定约束如何改变大小的行为。

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(通过对齐或者固定菜单来添加一个约束)

Select the checkbox next to the appropriate constraint.

不要识途去改变他人,同样,也不要被他人所改变。改了,就不是自己了。

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

相关文章:

你感兴趣的文章:

标签云: