frame、bounds和center

frame、bounds和center

frame:该view在父view坐标系统中的位置和大小
bounds:该view在本身坐标系统中的位置和大小
center:该view的中心点在父view坐标系统中的位置和大小

其实本地坐标系统的关键就是要知道他的原点(0, 0)在什么位置(这个位置是相对于上层view的本地坐标系统而言的,最上层view就是window,他的本地坐标系统原点就是屏幕的左上角了)。
通过修改view的bounds属性可以修改本地坐标系统的原点位置,进而影响到“子view”的显示位置。

UIView *viewA = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
[viewA setBounds:CGRectMake(-30, -30, 200, 200)];
viewA.backgroundColor = [UIColor redColor];
[self.view addSubview:viewA];

UIView *viewB = [[UIView alloc] initWithFrame:CGRectMake(-10, -10, 100, 100)];
viewB.backgroundColor = [UIColor yellowColor];
[viewA addSubview:viewB];

以上代码运行效果:

运行效果

bounds和frame的值:

viewA.frame:{{20, 20}, {200, 200}}, viewA.bounds:{{-30, -30}, {200, 200}}
viewB.frame:{{-10, -10}, {100, 100}}, viewB.bounds:{{0, 0}, {100, 100}}
[viewA setBounds:CGRectMake(-30, -30, 200, 200)];
修改为:
[viewA setBounds:CGRectMake(-30, -30, 245, 245)];

运行效果:

运行效果

bounds和frame的值:

viewA.frame:{{-2.5, -2.5}, {245, 245}}, viewA.bounds:{{-30, -30}, {245, 245}}
viewB.frame:{{-10, -10}, {100, 100}}, viewB.bounds:{{0, 0}, {100, 100}}

bounds可以通过修改自己坐标系原点的位置来影响“子view”的显示位置。
bounds可以改变frame,如果bounds比frame大,那么frame也会随之变大。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值