版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010209842/article/details/64611197
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = [[UIScreen mainScreen] bounds];
UIView *view1 = [[UIView alloc]init];
view1.frame = CGRectMake(100, 100, 100, 100);
view1.backgroundColor = [UIColor blackColor];
//允许子视图自适应
view1.tag = 100;
view1.autoresizesSubviews = YES;
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc]init];
view2.frame = CGRectMake(25, 25, 50, 50);
view2.backgroundColor = [UIColor whiteColor];
//设置子视图适应方式
view2.autoresizingMask =
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleBottomMargin ;
[view1 addSubview:view2];
UIButton *btn = [[UIButton alloc]init];
btn.frame = CGRectMake(0, frame.size.height-200, 50, 50);
btn.backgroundColor = [UIColor grayColor];
[btn setTitle:@"变化" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)click:(UIButton *)btn{
UIView *view1 = [self.view viewWithTag:100];
CGRect frame = view1.frame;
view1.frame = CGRectMake(frame.origin.x-20, frame.origin.y-20, frame.size.width+40, frame.size.height+40);
}
父级view1中,包含一个view2,view1的大小变化时,view2可以根据设定条件,与view1一同变化:
view1先设置属性 ,使子view可以动态改变大小:
view1.autoresizesSubviews =YES;
view2子控件设置autoresizeingMask属性,来决定父view变化,子view做出怎样相应的变化:
view2.autoresizingMask =
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleBottomMargin ;