重新布局子视图的情景
点击父视图放大时,子视图的布局并未发生相应的改变,此时需要重新布局子视图。(如下图所示)
iOS子视图重新布局分为手动布局和自动布局两种方式。
手动布局主要是通过重写layoutSubviews来实现,而自动布局主要通过设置视图的autoresizingMask属性来实现子视图的重新布局。
原视图
未重新布局子视图点击放大时
重新布局子视图点击放大时
手动布局子视图
重写layoutSubviews方法
//创建视图对象
- (void)creatSubViews{
_view01 = [[UIView alloc] init];
_view01.frame = CGRectMake(0, 0, 40, 40);
_view02 = [[UIView alloc] init];
_view02.frame = CGRectMake(XWIDTH - 40, 0, 40, 40);
_view03 = [[UIView alloc] init];
_view03.frame = CGRectMake(0, XHEIGHT - 40, 40, 40);
_view04 = [[UIView alloc] init];
_view04.frame = CGRectMake(XWIDTH - 40, XHEIGHT - 40, 40, 40);
_view01.backgroundColor = [UIColor orangeColor];
_view02.backgroundColor = [UIColor orangeColor];
_view03.backgroundColor = [UIColor orangeColor];
_view04.backgroundColor = [UIColor orangeColor];
[self addSubview:_view01];
[self addSubview:_view02];
[self addSubview:_view03];
[self addSubview:_view04];
_view05 = [[UIView alloc] init];
_view05.frame = CGRectMake(0, XHEIGHT / 2 - 20, XWIDTH, 40);
_view05.backgroundColor = [UIColor orangeColor];
[self addSubview:_view05];
}
//**************************手动布局实现********************//
//当需要重新布局时调用此函数
//通过此函数,重新设定子视图的位置
//手动调整子视图的位置
- (void)layoutSubviews{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
_view01.frame = CGRectMake(0, 0, 40, 40);
_view02.frame = CGRectMake(XWIDTH - 40, 0, 40, 40);
_view03.frame = CGRectMake(0, XHEIGHT - 40, 40, 40);
_view04.frame = CGRectMake(XWIDTH - 40, XHEIGHT - 40, 40, 40);
_view05.frame = CGRectMake(0, XHEIGHT / 2 - 20, XWIDTH, 40);
[UIView commitAnimations];
}
自动布局子视图
//创建视图对象
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_superView = [[UIView alloc] init];
_superView.frame = CGRectMake(20, 20, 180, 280);
_superView.backgroundColor = [UIColor blueColor] ;
_label01 = [[UILabel alloc] init];
_label01.frame = CGRectMake(0, 0, 40, 40);
_label01.text = @"1";
_label01.backgroundColor = [UIColor greenColor];
_label02 = [[UILabel alloc] init];
_label02.frame = CGRectMake(180 - 40, 0, 40, 40);
_label02.text = @"2";
_label02.backgroundColor = [UIColor greenColor];
_label03 = [[UILabel alloc] init];
_label03.frame = CGRectMake(180 -40, 280 - 40, 40, 40);
_label03.text = @"3";
_label03.backgroundColor = [UIColor greenColor];
_label04 = [[UILabel alloc] init];
_label04.frame = CGRectMake(0, 280 - 40, 40, 40);
_label04.text = @"4";
_label04.backgroundColor = [UIColor greenColor];
[self.view addSubview:_superView];
[_superView addSubview:_label01];
[_superView addSubview:_label02];
[_superView addSubview:_label03];
[_superView addSubview:_label04];
_viewCenter = [[UIView alloc] init];
_viewCenter.frame = CGRectMake(0, 0, _superView.bounds.size.width, 40);
_viewCenter.center = CGPointMake(180 / 2, 280 / 2);
_viewCenter.backgroundColor = [UIColor orangeColor];
[_superView addSubview:_viewCenter];
//*****************自动布局关键代码**********************//
//自动布局属性设置
//通过此变量来调整视图视图在父亲视图中的位置和大小
_viewCenter.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
//视图距离父亲视图的左侧可以调整
_label02.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
_label03.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
_label04.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
static BOOL _isLarge = NO;
// [UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
if (_isLarge) {
_superView.frame = CGRectMake(20, 20, 180, 280);
_isLarge = NO;
} else {
_superView.frame = self.view.bounds;
_isLarge = YES;
}
// [UIView commitAnimations];
}