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.
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.
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(居于容器视图中央),对齐两个视图的左边缘。
- 定住。创建距离约束,比如定义视图的高度,或者指定这个视图和两一个视图的水平距离
- 问题。通过根据建议添加或者设置约束来解决布局问题。(详见文章:《解决自动布局问题》)
- 改变大小。指定何种方式约束影响视图的改变。
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.
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.
你可以在任何时候通过在画布上或者大纲视图上选择约束并且按下删除键郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。