//bounds 是对象自身的起点(默认 0 ,0 )和宽高
//frame 是对象在父对象中的 位置 起点宽和高
//每个对象的bounds 都是 其子对象的 frame 坐标参照系 ,
由下面代码 可以看出 对象的bounds 的(0,0)点为其子对象的
frame的坐标原点,如果 bounds 不是(0,0)起点 那么创建
子对象的frame起点还是以(父对象bounds的原点(0,0)为原点)这样子对象
会根据原点进行偏移。
//总结成一句话就是 frame 和 bounds 相互限制 (指范围大小 根据谁定义在后决定)
相互关联(frame 决定 子级bounds在父级的位置 ,父级的bounds 又决定子级frame的坐标系),
形成一个汉堡包形态的bounds-frame-bounds-frame....bounds的界面关系,以bounds开头以bounds结尾。
UIView *view1 = [UIView new];
view1.frame = CGRectMake(50, 50, 50, 50);
view1.bounds = CGRectMake(20, 20, 100, 100);
view1.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view1];
UIView *view2 = [UIView new];
view2.frame = CGRectMake(10, 10, 30, 30);
view2.backgroundColor = [UIColor blackColor];
[view1 addSubview:view2];
NSLog(@"view de bounds and frame %@ %@",NSStringFromCGRect(view1.bounds),NSStringFromCGRect(view1.frame));
NSLog(@"view de bounds and frame %@ %@",NSStringFromCGRect(view2.bounds),NSStringFromCGRect(view2.frame));
结果
2016-03-21 21:09:59.493 2048UI版[3153:515742] view de bounds and frame {{20, 20}, {100, 100}} {{25, 25}, {100, 100}}
2016-03-21 21:09:59.494 2048UI版[3153:515742] view de bounds and frame {{0, 0}, {30, 30}} {{10, 10}, {30, 30}}
UIView *view1 = [UIView new];
view1.bounds = CGRectMake(20, 20, 100, 100);
view1.frame = CGRectMake(50, 50, 50, 50);
view1.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view1];
UIView *view2 = [UIView new];
view2.frame = CGRectMake(10, 10, 30, 30);
view2.backgroundColor = [UIColor blackColor];
[view1 addSubview:view2];
NSLog(@"view de bounds and frame %@ %@",NSStringFromCGRect(view1.bounds),NSStringFromCGRect(view1.frame));
NSLog(@"view de bounds and frame %@ %@",NSStringFromCGRect(view2.bounds),NSStringFromCGRect(view2.frame));
结果
2016-03-21 21:12:04.055 2048UI版[3171:517173] view de bounds and frame {{20, 20}, {50, 50}} {{50, 50}, {50, 50}}
2016-03-21 21:12:04.056 2048UI版[3171:517173] view de bounds and frame {{0, 0}, {30, 30}} {{10, 10}, {30, 30}}
本文深入探讨了UIKit中视图的坐标系统,特别是frame与bounds属性的区别及相互作用,通过具体的代码示例展示了如何设置这些属性来调整视图的位置与大小。
2444

被折叠的 条评论
为什么被折叠?



