标签:
@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
UIView 的属性autoresizesSubviews 默认设置是YES,如果设置为YES,当视图的bounds变化的时候,子视图将根据自身的autoresizingMask属性调节Frame属性。
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 };
1 #import "TestViewController.h" 2 @interface TestViewController() 3 @property (nonatomic,weak) UIView * superView; 4 @end 5 6 @implementation TestViewController 7 -(void)loadView 8 { 9 [super loadView]; 10 UIView * superView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 200)]; 11 superView.backgroundColor = [UIColor redColor]; 12 [self.view addSubview:superView]; 13 self.view.backgroundColor = [UIColor whiteColor]; 14 _superView = superView; 15 UIView * subView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 50, 40)]; 16 superView.autoresizesSubviews = YES; 17 18 // 1、设置subView随superView的bounds变化,上、下、左、右边距和宽高都随superView的变化而变化 19 subView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleWidth; 20 // 2、设置subView随superView的bounds变化,上、下、高都随superView的变化而变化,而左右边距和宽度不变 21 subView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin; 22 // 3、设置subView随superView的bounds变化,上边距和高都随superView的变化而变化,而下、左、右边距和宽度不变 23 subView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight; 24 // 4、单一属性一般不做此类设置(没看出到底什么效果) 25 // subView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; 26 // 5、设置subView随superView的bounds变化,下边距和高都随superView的变化而变化,而上、左、右边距和宽度不变 27 subView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin; 28 // 6、设置subView随superView的bounds变化,左边距和宽都随superView的变化而变化,而上、下、右边距和高度不变 29 subView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin; 30 // 7、设置subView随superView的bounds变化,右边距和宽都随superView的变化而变化,而上、下、左边距和高度不变 31 subView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin; 32 // 8、设置subView随superView的bounds变化,左、右边距和宽都随superView的变化而变化,而上、下边距和高度不变 33 subView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin; 34 35 36 subView.backgroundColor = [UIColor blackColor]; 37 [superView addSubview:subView]; 38 39 UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 400,100,100)]; 40 [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; 41 [btn setTitle:@"点我" forState:UIControlStateNormal]; 42 [btn setBackgroundColor:[UIColor grayColor]]; 43 44 [self.view addSubview:btn]; 45 } 46 47 -(void)click:(id)sender 48 { 49 _superView.frame = CGRectMake(50, 50, 200, 200); 50 } 51 @end
superView开始的frame为(20, 20, 100, 200)点击按钮设置为(50, 50, 200, 200)
subView的frame为(0, 20, 50, 40)
subView开始距离superView上边距为20
subView开始距离superView左边距为0
subView开始距离superView下边距为140
subView开始距离superView右边距为50
调整后superView的frame为(50, 50, 200, 200)后,subView跟随变化。
superView和之前frame相比,高度无变化,那么subView相对于superView在上下边距和高度上自然就没有相应的变化。
而宽度从100变为200,变大了一倍,那么相应的subView相对于superView在左右边距和宽度上也要增加一倍。
subView距离superView上边距依旧为20*1
subView距离superView左边距为0*2
subView距离superView下边距为140*1
subView距离superView右边距为50*2
subView的高度为40*1
subView的宽度为50*2
superView开始的frame为(20, 20, 100, 200)点击按钮设置为(50, 50, 200, 200)
subView的frame为(0, 20, 50, 40)
subView开始距离superView上边距为20
subView开始距离superView左边距为0
subView开始距离superView下边距为140
subView开始距离superView右边距为50
调整后superView的frame为(50, 50, 200, 200)后,subView跟随变化。
superView和之前frame相比,高度无变化,那么subView相对于superView在上下边距和高度上自然就没有相应的变化。
而宽度从100变为200,变大了一倍,且subView和superView的右边距要保持不变左边距和宽度就需要重新计算。
那么从100变为200的实际变化其实就要忽略原有的右边距(其实可以这样去理解,不跟随变化的属性在计算过程中直接减掉对应值在计算变化的倍数)那么就应该是从(100-50)-(200-50)=1/3那么右边距和宽度都应该是之前变化的3倍数。
*(100-50)原来宽度减去固定的距右边距;(200-50)变化后宽度减去固定的距右边距。
subView距离superView上边距依旧为20*1
subView距离superView左边距为0*3
subView距离superView下边距为140*1
subView距离superView右边距为50*1
subView的高度为40*1
subView的宽度为50*3
以上为本人对autoresizingMask属性的理解,如果有错误或者不足的地方欢迎指出。(也可加本人QQ627306590交流,望多多指教。)
iOS8 UIView之autoresizingMask 的使用
标签:
原文地址:http://www.cnblogs.com/Zerone-Lee/p/4570380.html