(一) autoresizing 简介
autoresize的使用
autoresizing 和 autolayout 是 互斥的
-
autoresizing 外四根, 内 两根
外四根: 控制子view 到父view边界的距离是否变化
内两根: 子view的size 是否会跟随父viewsize的变化而变化
- 位枚举, 可以同时设置 多个值
view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
3.autoresizing 的弊端 , 不能设置 任意子控件之间的参照(约束)
(二) 代码示例
-(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 进行设置
/**
位枚举, 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
*/
/**
左侧保持不变 - 右侧灵活
右侧保持不变 - 左侧灵活
底部保持不变
宽度保持变化
*/
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;
}