frame 和 bounds 的关系

本文深入探讨了UIKit中视图的坐标系统,特别是frame与bounds属性的区别及相互作用,通过具体的代码示例展示了如何设置这些属性来调整视图的位置与大小。

//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(50505050);

    view1.bounds = CGRectMake(2020100100);

    

    view1.backgroundColor = [UIColor orangeColor];

    [self.view addSubview:view1];

    UIView *view2 = [UIView new];

    view2.frame = CGRectMake(10103030);

    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(2020100100);

     view1.frame = CGRectMake(50505050);

    view1.backgroundColor = [UIColor orangeColor];

    [self.view addSubview:view1];

    UIView *view2 = [UIView new];

    view2.frame = CGRectMake(10103030);

    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}}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值