UIView 的 autoresizingMask 属性 详解。

来源:互联网 发布:淘宝上传宝贝数量限制 编辑:程序博客网 时间:2024/05/07 20:16

在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高。

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


分别解释以上意思。

UIViewAutoresizingNone就是不自动调整。

UIViewAutoresizingFlexibleLeftMargin就是自动调整与superView左边的距离,也就是说,与superView右边的距离不变。

UIViewAutoresizingFlexibleRightMargin就是自动调整与superView的右边距离,也就是说,与superView左边的距离不变。

UIViewAutoresizingFlexibleTopMargin

UIViewAutoresizingFlexibleBottomMargin

UIViewAutoresizingFlexibleWidth

UIViewAutoresizingFlexibleHeight

以上就不多解释了,参照上面的。

也可以多个枚举同时设置。如下:

subView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin  |UIViewAutoresizingFlexibleRightMargin;

如果有多个,就用“|”关联。

还有一个属性就是autoresizesSubviews,此属性的意思就是,是否可以让其subviews自动进行调整,默认状态是YES,就是允许,如果设置成NO,那么subView的autoresizingMask属性失效。


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

GaoYP经过详细测试,终于验证了这几个值的意义,并发现网上找到的资料居然全都是错的。汗一个,原来大家都是互相抄,也没有检查过。

因为横向和纵向的变换方式是一样的,所以就以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.