UIView的autoresizingMask详解

来源:互联网 发布:重庆双成网络 编辑:程序博客网 时间:2024/05/19 22:02

enum {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};

因为横向和纵向的变换方式是一样的,所以就以iPhone中更常用的纵向变换为例了:

UIViewAutoresizingNone:superview变换时,自己不作变换。

UIViewAutoresizingFlexibleHeight:上边距不变,和superview在高度上变换同等高度。
比如,superview加高100,则自己也加高100。

UIViewAutoresizingFlexibleTopMargin:高度不变。上边距弹性可变,下边距保持不变。

UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight:
这个组合的变换比较绕:
首先,下边距是不变的,但高和上边距会变,变换的计算如下,
比如superview的高度,由100加高的200。自己的下边距是50,
则去掉不变的下边距后,superview的变化比例是:(100-50)/(200-50) = 50/150 = 1/3。
则自己的上边距和高都变为越来的3倍。

UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin :
这个变换的计算就比较简单了,救是自己的上边距,高,下边距都和superview同比变换。
比如superview的高由100变为200。则自己的上边距,高,下边距也都变为原来的2倍。

上面的变换只在superview的autoresizesSubviews为YES是才会发生。autoresizesSubviews默认为YES。

同时,superview的contentMode不会影响sub view的变换。

下面是苹果的官方文档:

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/autoresizingMask

autoresizesSubviews
A Boolean value that determines whether the receiver automatically resizes its subviews when its bounds change.

@property(nonatomic) BOOL autoresizesSubviews
Discussion
When set to YES, the receiver adjusts the size of its subviews when its bounds change. The default value is YES.

Availability
Available in iOS 2.0 and later.
See Also
@property autoresizingMask
Declared In
UIView.h
autoresizingMask
An integer bit mask that determines how the receiver resizes itself when its superview’s bounds change.

@property(nonatomic) UIViewAutoresizing autoresizingMask
Discussion
When a view’s bounds change, that view automatically resizes its subviews according to each subview’s autoresizing mask. You specify the value of this mask by combining the constants described in UIViewAutoresizing using the C bitwise OR operator. Combining these constants lets you specify which dimensions of the view should grow or shrink relative to the superview. The default value of this property is UIViewAutoresizingNone, which indicates that the view should not be resized at all.

When more than one option along the same axis is set, the default behavior is to distribute the size difference proportionally among the flexible portions. The larger the flexible portion, relative to the other flexible portions, the more it is likely to grow. For example, suppose this property includes the UIViewAutoresizingFlexibleWidth and UIViewAutoresizingFlexibleRightMargin constants but does not include the UIViewAutoresizingFlexibleLeftMargin constant, thus indicating that the width of the view’s left margin is fixed but that the view’s width and right margin may change. Thus, the view appears anchored to the left side of its superview while both the view width and the gap to the right of the view increase.

If the autoresizing behaviors do not offer the precise layout that you need for your views, you can use a custom container view and override its layoutSubviews method to position your subviews more precisely.

Availability
Available in iOS 2.0 and later.

0 0