UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
[view2 setBackgroundColor:[UIColor grayColor]];
[view1 addSubview:view2];
frame of view2:{{0, 0}, {200, 200}}, bounds of view2:{{0, 0}, {200, 200}}
不设置bounds 则bounds默认为(0,0) size和frame的size一样大小。
frame设置为(0,0) size为(200,200) frame的(0,0) 取决于view1的bounds设置
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
[view1 setBounds:CGRectMake(-20, -20, 100, 100)];
[view1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:view1];
frame of view1:{{0, 0}, {100, 100}}, bounds of view1:{{-20, -20}, {100, 100}}
view1设置了frame和bounds。frame的size和bounds的size一样大小。
则frame的(0,0)决定了view1的位置。
红色视图在屏幕左上角。宽高为100pt。
view1.bounds(-20,-20)决定了红色视图内部坐标系统。
在红色视图内 左上角为(-20,-20) 所以(0,0)往右下角时钟4点半方向移动。
view2的frame承担影响。
frame中的(x,y)参照的是父视图的坐标系
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
NSLog(@"==11==frame of view1:%@, bounds of view1:%@ center of view1:%@",NSStringFromCGRect(view1.frame), NSStringFromCGRect(view1.bounds), NSStringFromCGPoint(view1.center));
[view1 setBounds:CGRectMake(-200, -200, 200, 200)];
[view1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:view1];
NSLog(@"==22==frame of view1:%@, bounds of view1:%@ center of view1:%@",NSStringFromCGRect(view1.frame), NSStringFromCGRect(view1.bounds), NSStringFromCGPoint(view1.center));
UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
[view2 setBackgroundColor:[UIColor grayColor]];
[view1 addSubview:view2];
NSLog(@"frame of view2:%@, bounds of view2:%@",NSStringFromCGRect(view2.frame), NSStringFromCGRect(view2.bounds));
==11==frame of view1:{{50, 50}, {100, 100}}, bounds of view1:{{0, 0}, {100, 100}} center of view1:{100, 100}
==22==frame of view1:{{0, 0}, {200, 200}}, bounds of view1:{{-200, -200}, {200, 200}} center of view1:{100, 100}
frame of view2:{{0, 0}, {200, 200}}, bounds of view2:{{0, 0}, {200, 200}}
view1先设置frame再设置bounds并且2次size不一样。
frame(50, 50, 100, 100)
宽高100。左上角(50,50) 则有了center在(100,100)
在设置bounds发现size不一样了。中心点不变。size得刷新下。200宽高一更新。
红色方块到了左上角。
Screen Shot 2021-04-25 at 18.27.37.png
网友评论