本文主要是介绍UIView 子view跟随父view动态变化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- (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 ;
这篇关于UIView 子view跟随父view动态变化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!