bounds 与frame的区别

    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 100)];

    view1.backgroundColor = [UIColor blueColor];

    view1.bounds = CGRectMake(0, 100, 100, 100);

    [self.view addSubview:view1];

    

    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 110, 50, 50)];

      view2.bounds = CGRectMake(0, 0, 50, 50);

//      view2.bounds = CGRectMake(0, 50, 50, 20);

    view2.backgroundColor = [UIColor redColor];

    [view1 addSubview:view2];

    NSLog(@"bounds={%@} center={%@}",NSStringFromCGPoint(view1.bounds.origin),NSStringFromCGPoint(view1.frame.origin));

        NSLog(@"bounds={%@} center={%@}",NSStringFromCGPoint(view2.bounds.origin),NSStringFromCGPoint(view2.frame.origin));


蓝色为A视图,红色为B视图,A视图的bounds为 B加入到A   Abounds为0,100, B设置frame时,如果要从A的原点开始设置就要设置为0,100 如果要设置相对A视图以下10个像素,就要设置为A的bounds 加+相对坐标。

这样理解起来就容易多了。总结一句就是,bounds 是自己的坐标系,要在这个坐标系上加入子view 就要设置相对坐标,这个相对坐标是更根据父视图的bounds 坐标来的,初始化时bounds都为0 所以我们就可以用普通的相对值来设置frame。

转载于:https://my.oschina.net/u/1049180/blog/282464

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值