iOS brounds和frame的区别

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:就是该视图在自己这套坐标系中的位置以及大小,只会影响自身子视图的位置。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容