(一) autoresizing 簡介
autoresize的使用
autoresizing 和 autolayout 是 互斥的
-
autoresizing 外四根, 內(nèi) 兩根
外四根: 控制子view 到父view邊界的距離是否變化
內(nèi)兩根: 子view的size 是否會跟隨父viewsize的變化而變化
- 位枚舉, 可以同時設(shè)置 多個值
view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
3.autoresizing 的弊端 , 不能設(shè)置 任意子控件之間的參照(約束)
(二) 代碼示例
-(void)viewDidLoad {
[super viewDidLoad];
// 實例化兩個view
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
self.redView = redView;
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 200, 50)];
blueView.backgroundColor = [UIColor blueColor];
[redView addSubview:blueView];
// 對 blueView 進行設(shè)置
/**
位枚舉, option 枚舉
Flexible : 靈活的 , 可變化的
UIViewAutoresizingNone = 0, 0
0000 0000
UIViewAutoresizingFlexibleLeftMargin = 1 << 0, 1
0000 0001 --> 1 * 2 0次方
UIViewAutoresizingFlexibleWidth = 1 << 1, 2
0000 0010
UIViewAutoresizingFlexibleRightMargin = 1 << 2, 4
0000 0100
UIViewAutoresizingFlexibleTopMargin = 1 << 3, 8
0000 1000
UIViewAutoresizingFlexibleHeight = 1 << 4, 16
0001 0000
UIViewAutoresizingFlexibleBottomMargin = 1 << 5 32
0010 0000
*/
/**
左側(cè)保持不變 - 右側(cè)靈活
右側(cè)保持不變 - 左側(cè)靈活
底部保持不變
寬度保持變化
*/
blueView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGRect tempBounds = _redView.bounds;
tempBounds.size.width += 20;
tempBounds.size.height += 20;
_redView.bounds = tempBounds;
}