frame和bounds的区别

  • frame:根据父视图坐标系来确定自己的位置
  • bounds:该视图在自己坐标系的位置和大小
  • 修改bounds并不会引起视图位置的变化,会影响自身子视图的位置;修改frame会引起视图位置的变化
1 UIView *view1 = [[UIView alloc] init];
2 view1.frame = CGRectMake(10, 10, 200, 200);
3 [self.wiew addSubview:view1];
4 
5 UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
6     view2.backgroundColor = [UIColor yellowColor];
7     [view1 addSubview:view2];

这时显示的视图是

此时没有设置bounds,View1自己的坐标远点是(0,0),这是添加View2时,以(0,0)为原点计算偏移(50,50);

1 UIView *view1 = [[UIView alloc] init];
2 view1.frame = CGRectMake(10, 10, 200, 200);
3 view1.bounds = CGRectMake(50, 50, 200, 200);
4 [self.wiew addSubview:view1];
5 
6 UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
7     view2.backgroundColor = [UIColor yellowColor];
8     [view1 addSubview:view2];

这时显示的视图是

此时View1设置里bounds,自己的坐标原点变为(50,50),添加View2时,以(50,50)为原点计算偏移

posted @ 2016-06-03 11:00  Leo-Wmw  阅读(252)  评论(0)    收藏  举报