您的位置:首页 > 移动开发 > IOS开发

[解析苹果官方文档]之[自动布局-view.autoresizingMask]

2016-01-26 15:45 671 查看
Whenever the size of a view changes, the size and position of its subviews must change accordingly. The
UIView
class supports both the automatic and manual layout of views in a view hierarchy. With automatic layout, you set the rules that each view should follow when its parent view resizes, and then forget about resizing operations altogether. With manual layout, you manually adjust the size and position of views as needed. 每当某个视图的大小发生变化时,其子视图的位置及大小都应发生相应的改变。UIView类对视图层次中每个视图的布局都提供了两种支持:自动布局,以及手动布局。当父视图的大小发生改变时,我们可以通过自动布局方式来设置其子视图大小变化的规则,之后就不用我们操太多的心了。而手动布局方式则需要我们按照需求手动调整每个子视图的位置及大小。

Being Prepared for Layout Changes

Layout changes can occur whenever any of the following events happens in a view:The size of a view’s bounds rectangle changes.
An interface orientation change occurs, which usually triggers a change in the root view’s bounds rectangle.
The set of Core Animation sublayers associated with the view’s layer changes and requires layout.
Your application forces layout to occur by calling the
setNeedsLayout
or
layoutIfNeeded
method of a view.
Your application forces layout by calling the
setNeedsLayout
method of the view’s underlying layer object.
在哪些情况下,我们需要调整子视图的布局呢?1. view.bounds.size变了;2. 界面走向变了(如横竖屏切换), 这种情况会引起根视图的bounds属性发生改变;3. 与view.layer相关的Core Animation sublayer变了,需要重新布局;4. 你的应用通过调用view的 setNeedsLayout 或 layoutIfNeeded方法,强制重新布局;5. 你的应用通过调用view.layer的 setNeedsLayout方法,强制重新布局。

Handling Layout Changes Automatically Using Autoresizing Rules

When you change the size of a view, the position and size of any embedded subviews usually needs to change to account for the new size of their parent. The
autoresizesSubviews
property of the superview determines whether the subviews resize at all. If this property is set to
YES
, the view uses the
autoresizingMask
property of each subview to determine how to size and position that subview. Size changes to any subviews trigger similar layout adjustments for their embedded subviews.For each view in your view hierarchy, setting that view’s
autoresizingMask
property to an appropriate value is an important part of handling automatic layout changes. Table 3-2 lists the autoresizing options you can apply to a given view and describes their effects during layout operations. You can combine constants using an OR operator or just add them together before assigning them to the
autoresizingMask
property. If you are using Interface Builder to assemble your views, you use the Autosizing inspector to set these properties. 运用autoresizing规则自动适应布局变化: 在你改变了某个视图的大小时,通常嵌于此视图之上的所有子视图的位置和大小都需要做改变以适应父视图。父视图的autoresizesSubviews属性从根本上决定了子视图是否能够重新布局。若将此属性值设置为yes,那么父视图会根据每个子视图的autoresizingMask属性值来决定如何调整子视图的位置及大小。而子视图大小的改变,随之也会令这些子视图的子视图进行类似的布局调整。以此类推。 对于视图层级中的每个视图来说,设置其autoresizingMask属性是应对自动布局变化的重要一步。下图列出了可用的view.autoresizingMask值;我们可以用或运算符、加法运算符把这些属性值常量结合起来赋值给属性。如果你使用了故事版搭建视图, 那么要用Size Inspector->Autoresizing板块(见下图2)设置这个属性。(由于项目默认使用了Autolayout,因此需要先在File Inspector的Interface Builder Document板块下取消勾选User Auto Layout框,见下图1。)


图1



图2

Autoresizing属性的掩码常量:
Table 3-2 Autoresizing mask constants
Autoresizing maskDescription
UIViewAutoresizingNone
The view does not autoresize. (This is the default value.)
UIViewAutoresizingFlexibleHeight
The view’s height changes when the superview’s height changes. If this constant is not included, the view’s height does not change.
UIViewAutoresizingFlexibleWidth
The view’s width changes when the superview's width changes. If this constant is not included, the view’s width does not change.
UIViewAutoresizingFlexibleLeftMargin
The distance between the view’s left edge and the superview’s left edge grows or shrinks as needed. If this constant is not included, the view’s left edge remains a fixed distance from the left edge of the superview.
UIViewAutoresizingFlexibleRightMargin
The distance between the view’s right edge and the superview’s right edge grows or shrinks as needed. If this constant is not included, the view’s right edge remains a fixed distance from the right edge of the superview.
UIViewAutoresizingFlexibleBottomMargin
The distance between the view’s bottom edge and the superview’s bottom edge grows or shrinks as needed. If this constant is not included, the view’s bottom edge remains a fixed distance from the bottom edge of the superview.
UIViewAutoresizingFlexibleTopMargin
The distance between the view’s top edge and the superview’s top edge grows or shrinks as needed. If this constant is not included, the view’s top edge remains a fixed distance from the top edge of the superview.
Figure 3-4 shows a graphical representation of how the options in the autoresizing mask apply to a view. The presence of a given constant indicates that the specified aspect of the view is flexible and may change when the superview’s bounds change. The absence of a constant indicates that the view’s layout is fixed in that aspect. When you configure a view that has more than one flexible attribute along a single axis, UIKit distributes any size changes evenly among the corresponding spaces. 下图给出了这些属性值的应用场景图示:1)某个常量的出现,意味着当父视图的bounds改变时,子视图相应的元素可灵活变更 : (例如:view.autoresizingMask=UIViewAutoresizingFlexibleHeight ,这代表了视图的高度可灵活变更 )2)某个常量的缺省,意味着子视图相应的元素保持不变: (例如:view.autoresizingMask=UIViewAutoresizingFlexibleHeight ,属性值(沿y轴方向)中没有出现UIViewAutoresizingFlexibleBottomMargin 或 UIViewAutoresizingFlexibleTopMargin ,这代表了视图的底部间隔和顶部间隔都应保持不变。)
3)当沿着某个坐标轴设置的可变(flexible)属性值大于2个时,则会等比例增减相应的空间大小: (例如:view.autoresizingMask=UIViewAutoresizingFlexibleHeight || UIViewAutoresizingFlexibleTopMargin,这代表了视图的底部间隔保持不变,高度和顶部间隔等比例增减 )Figure 3-4 View autoresizing mask constants


The easiest way to configure autoresizing rules is using the Autosizing controls in the Size inspector of Interface Builder. The flexible width and height constants from the preceding figure have the same behavior as the width and size indicators in the Autosizing controls diagram. However, the behavior and use of margin indicators is effectively reversed. In Interface Builder, the presence of a margin indicator means that the margin has a fixed size and the absence of the indicator means the margin has a flexible size. Fortunately, Interface Builder provides an animation to show you how changes to the autoresizing behaviors affect your view. 配置自动布局autoresizing的规则时,最简单的方法是:用interface builder -> size inspector -> autoresizing 板块。
这里需要注意的是:
1) flexible width / height indicator 设置为实线时为可变,虚线时为固定;
2) flexbile top / bottom / left / right indicator 设置为实线时为固定,虚线时为可变。
还好 interface builder提供了动画展示,你可以随意进行设置,并观察所带来的相应动态改变。

Important: If a view’s
transform
property does not contain the identity transform, the frame of that view is undefined and so are the results of its autoresizing behaviors. 重要提示:如果视图的transform属性值
(CGAffineTransform:
A structure for holding an affine transformation matrix. 结构体,用来存放transform矩阵的9个数值. ) 不是identity transform(见下图),那么view的frame属性处于未定义状态,其autoresizing自动布局的效果也是不确定的。The identity transform:

(关于此矩阵的用法,参见文档的[ The Transform Matrix ]章节。)

After the automatic autoresizing rules for all affected views have been applied, UIKit goes back and gives each view a chance to make any necessary manual adjustments to its superview. For more information about how to manage the layout of views manually, see Tweaking the Layout of Your Views Manually. 在应用了这些自动布局规则后,uikit沿view层级从子视图回退到父视图,从而有机会在父视图层进行手动布局再调整。(见下)

Tweaking the Layout of Your Views Manually

Whenever the size of a view changes, UIKit applies the autoresizing behaviors of that view’s subviews and then calls the
layoutSubviews
method of the view to let it make manual changes. You can implement the
layoutSubviews
method in custom views when the autoresizing behaviors by themselves do not yield the results you want. Your implementation of this method can do any of the following:Adjust the size and position of any immediate subviews.
Add or remove subviews or Core Animation layers.
Force a subview to be redrawn by calling its
setNeedsDisplay
or
setNeedsDisplayInRect:
method.
视图布局的手动微调:当某个view大小改变时,UIKit采用了子视图的autoresizing行为,并调用view的layoutSubviews方法,支持手动调整:如果autoresizing仍然达不到我们想要的效果,那么我们可以在自定义的视图类中重写layoutSubviews方法。在这个方法中我们可做的操作包括:1. 调整第一级子视图的位置及大小;2. 增/删子视图 或 CALayer;3. 调用setNeedsDisplaysetNeedsDisplayInRect方法,强制重新绘制子视图。
注:关于 iOS中AutoLayer自动布局流程及相关方法,见:http://my.oschina.net/w11h22j33/blog/208574
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息