UIView的autoresizingMask和autoresizesSubviews属性的剖析

UIVIew的autoresizingMask和autoresizesSubviews属性的剖析

autoresizingMask是为了iPad开发中横竖屏适配而降生的,他只能约束父子控件之间的关系。autoresizingMask和storyBoard中尺寸检查器中的Autoresizing是指的一回事。iPhone5开始,Xcode添加了autolayout功能。storyBoard默认采用autolayout,,取代了之前的autoresizing。如果使用autoresizing,需要在以下位置去掉“Use Auto Layout”。

和frame、bounds、center、transform等属性一样,autoresizingMask和autoresizesSubviews也是属于UIView的几何分类-UIViewGeometry中的属性。

@property(nonatomic) BOOL               autoresizesSubviews; // default is YES. if set, subviews are adjusted according to their autoresizingMask if self.bounds changes
@property(nonatomic) UIViewAutoresizing autoresizingMask;    // simple resize. default is UIViewAutoresizingNone

查看注释,autoresizesSubviews属性的大意是:默认autoresizesSubviews = YES。如果UIView设置了autoresizesSubviews,那么他的子控件的bounds如果发生了变化,他的子控件将会根据子控件自己的autoresizingMask属性的值来进行调整。

那么autoresizingMask又是神马呢?

autoresizingMask是一个枚举值,作用是自动调整子控件与父控件中间的margin(间距)或者子控件的宽高。默认其枚举值是UIViewAutoresizingNone。如下是其全部的枚举值:

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

 

下面对这几个枚举值进行一一说明:

  UIViewAutoresizingNone就是不自动调整。
  UIViewAutoresizingFlexibleLeftMargin 自动调整与superView左边的距离,保证与superView右边的距离不变。
  UIViewAutoresizingFlexibleRightMargin 自动调整与superView的右边距离,保证与superView左边的距离不变。
  UIViewAutoresizingFlexibleTopMargin 自动调整与superView顶部的距离,保证与superView底部的距离不变。
  UIViewAutoresizingFlexibleBottomMargin 自动调整与superView底部的距离,也就是说,与superView顶部的距离不变。
  UIViewAutoresizingFlexibleWidth 自动调整自己的宽度,保证与superView左边和右边的距离不变。
  UIViewAutoresizingFlexibleHeight 自动调整自己的高度,保证与superView顶部和底部的距离不变。


UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin 自动调整与superView左边的距离,保证与左边的距离和右边的距离和原来距左边和右边的距离的比例不变。比如原来距离为20,40,调整后的距离应为50,100,即50/20=100/40。

其它的组合类似。

注意:UIView的autoresizesSubviews属性为YES时(默认为YES),autoresizing才会生效。

文章参考自:http://www.cnblogs.com/jiangyazhou/archive/2012/06/26/2563041.html

附(苹果设备适配技术发展历史):

                附图:苹果设备适配技术发展历史

 

posted @ 2015-10-06 14:58  oneSong  阅读(1299)  评论(0编辑  收藏  举报