UIView的autoresizingMask

来源:互联网 发布:天猫超市满99减50知乎 编辑:程序博客网 时间:2024/05/16 18:44

UIViewAutoresizingNone                 = 0, 

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

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

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

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

UIViewAutoresizingFlexibleWidth:控件的宽度随着父视图的宽度按比例改变;

例如:label宽度为100,屏幕的宽度为320。当屏幕宽度为480时,label宽度变为100*480/320

UIViewAutoresizingFlexibleHeight:与UIViewAutoresizingFlexibleWidth相同

UIViewAutoresizingFlexibleLeftMargin:到屏幕左边的距离随着父视图的宽度按比例改变;

例如:CGRectMake(50, 100, 200, 40)]; 当屏幕的宽度为320,x为50;当屏幕宽度为480时,labelx坐标变为 50*480/320。控件坐标变为 CGRectMake(75, 100, 200, 40)];

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的变换。 

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


原创粉丝点击