IOS 自动布局指南2 自动布局概念(Auto Layout Concepts)fds

Anattributeis one ofleft,right,top,bottom,leading,trailing,width,height,centerX,centerY, andbaseline.

The attributesleadingandtrailingare the same asleftandrightfor left-to-right languages such as English, but in a right-to-left environment such as Hebrew or Arabic,leadingandtrailingare the same asrightandleft. When you create constraints,leadingandtrailingare the default values. You should usually useleadingandtrailingto make sure your interface is laid out appropriately in all languages, unless you’re making constraints that should remain the same regardless of language (such as the order of master and detail panes in a split view).

属性为如下之一:左,右,上,下,领先,落后,宽,搞,,中心X,中心Y,基线。

领先(leading)和落后(trailing)属性就像左和右对于左右语言像英语,但是对于右左语境的语言像希伯来语和阿拉伯语,(leading)领先和落后(trailing)就像右左一样。当你创建约束的时候,领先和落后是默认值。你应该总是使用leading和trailing来保证适合所有的语言,除非你做这样的限制:所有语言保持一致(就像……不知道怎么翻译)。

Constraints can have other properties set:

Constant value.The physical size or offset, in points, of the constraint.

Relation.Auto Layout supports more than just constant values for view attributes; you can use relations and inequalities such as greater-than-or-equal to specify, for example, that a view’swidth >= 20, or even thattextview.leading >= (superview.leading + 20).

Priority level.Constraints have a priority level. Constraints with higher priority levels are satisfied before constraints with lower priority levels. The default priority level is required (NSLayoutPriorityRequired), which means that the constraint must be satisfied exactly. The layout system gets as close as it can to satisfying an optional constraint, even if it cannot completely achieve it.

Priority levels allow you to express useful conditional behavior. For example, they are used to express that some controls should always be sized to fit their contents, unless something more important should take precedence. For more information about priority levels, seeNSLayoutPriority.

约束可以有其他属性设置:

常量。物理大小或相对原点偏移量,在约束(constraint)的points里面

关系。自动布局不仅仅支持视图属性的常量;你可以使用关系和不等式比如View的width>=20,或者 textview.leading>=(superview.leading+20)

优先级。约束有优先级。具有高优先级的约束比低优先级的约束先实现。默认的优先级是必要级(NSLayoutPriorityRequired),这意味着约束必须被完全满足(完全实现)。如果一个约束的优先级是选择完成,那么布局系统会尽量完成的接近这个约束,即使这个约束不能被完全实现。

优先级的存在允许你表达有用的约束行为。比如,他们常用来表达某些控件应该适应内容的大小,除非更重要的事情要被优先做。更多关于优先级的信息,请看《NSLayoutPriority》

Constraints are cumulative, and do not override each other. If you have an existing constraint, setting another constraint of the same type does not override the previous one. For example, setting a second width constraint for a view does not remove or alter the first width constraint—you need to remove the first constraint manually.

Constraints can, with some restrictions, cross the view hierarchy. In the Mail app in OSX, for example, by default the Delete button in the toolbar lines up with the message table; in Desktop Preferences, the checkboxes at the bottom of the window align with the split view pane they operate on.

You cannot set a constraint to cross the view hierarchy if the hierarchy includes a view that sets the frames of subviews manually in a custom implementation for themethod on(or thelayoutmethod onNSView). It’s also not possible to cross any views that have a bounds transform (such as a scroll view). You can think of such views as barriers—there’s an inside world and an outside world, but the inside cannot be connected to the outside by constraints.

约束的作用是累计的,他们并不会相互覆盖。如果你有一个现成的约束,又设置了另一个相同类型的约束并不会覆盖掉先前的约束。举个例子,你给一个View第二次设置了宽度约束,这并不会删除或者改变你第一次设置的宽度约束——你需要手动删除第一个约束。

约束可以有限制的跨越视图层次。在OSX 的邮件应用中,举个例子,(例子看不懂,不翻译)

你不能设置穿越视图层次的约束,如果这层次里面包含了一个通过手动实现UIView中layoutSubviews 方法(或者NSView 中的layout 方法)来设置子视图的View。当视图有bounds 变化的时候也不能穿越层次设置(比如scroll View)。你可以认为这些视图有内部壁垒,这些视图有内在世界和外在世界,可是两个世界不能通过约束联通。

Intrinsic Content Size (内在内容的大小)人生就是要感受美丽的善良的,丑恶的病态的。

IOS 自动布局指南2 自动布局概念(Auto Layout Concepts)fds

相关文章:

你感兴趣的文章:

标签云: