iOS brounds和frame的区别

2016-06-28  本文已影响26人  Lambo316

1.在view1视图上以view1.frame的格式添加view2视图

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];

view1.backgroundColor = [UIColor greenColor];

[self.window addSubview:view1];

UIView *view2 = [[UIView alloc] initWithFrame:view1.frame];

view2.backgroundColor = [UIColor redColor];

[view1 addSubview:view2];

解答:

①、view1的是将手机屏幕的左上角作为原点

②、view2则是将view1的左上角作为原点

③、可以理解为:view2以view1作为父视图,然后添加

view1视图上一view1.brounds的格式添加视图

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];

view1.backgroundColor = [UIColor greenColor];

[self.window addSubview:view1];

UIView *view2 = [[UIView alloc] initWithFrame:view1.bounds];

view2.backgroundColor = [UIColor redColor];

[view1 addSubview:view2];

解答:

①、此时view1和view2都是以屏幕的左上角作为原点

②、通俗点说就是view2是在view1视图上拷贝了view1,然后再view1所在的位置上黏贴了view2

3、在self.window视图上以view1.bounds格式添加view2

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 0, 200, 200)];

view1.backgroundColor = [UIColor greenColor];

[self.window addSubview:view1];

UIView *view2 = [[UIView alloc] initWithFrame:view1.bounds];

view2.backgroundColor = [UIColor redColor];

[self.window addSubview:view2];

view2.alpha = 0.3;

解答:

①、此时的屏幕原本的原点有(0,0),改成了(-50,0);

②、也可以说是将view1的x坐标和y坐标取相反数作为self.window的原点

4、在self.widow视图上一view1.frame格式添加view2

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 0, 200, 200)];

view1.backgroundColor = [UIColor greenColor];

[self.window addSubview:view1];

UIView *view2 = [[UIView alloc] initWithFrame:view1.frame];

view2.backgroundColor = [UIColor redColor];

[self.window addSubview:view2];

view2.alpha = 0.3;

解答:

①、以view1的创建格式做为view2的格式添加view2

②、通俗说就是view2在view1的位置上拷贝了view1

rame:是基于它父视图的坐标系而言

frame:是基于它父视图的坐标系而言

bounds:就是该视图在自己这套坐标系中的位置以及大小,只会影响自身子视图的位置。

上一篇 下一篇

猜你喜欢

热点阅读