frame、bounds、center以及frame和bounds的区别

UIView *test_View = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 100, 100)];
    test_View.backgroundColor = [UIColor redColor];
    [self.view addSubview:test_View];
    
    NSLog(@"frame:%.2f %.2f %.2f %.2f",test_View.frame.origin.x,test_View.frame.origin.y,test_View.frame.size.width,test_View.frame.size.height);
    NSLog(@"bounds:%.2f %.2f %.2f %.2f",test_View.bounds.origin.x,test_View.bounds.origin.y,test_View.bounds.size.width,test_View.bounds.size.height);
    NSLog(@"center:%.2f %.2f",test_View.center.x,test_View.center.y);

输出:

2018-03-20 15:40:41.061000+0800 frame_bounds_center[3431:215798] frame:40.00 40.00 100.00 100.00
2018-03-20 15:40:41.061125+0800 frame_bounds_center[3431:215798] bounds:0.00 0.00 100.00 100.00
2018-03-20 15:40:41.061218+0800 frame_bounds_center[3431:215798] center:90.00 90.00

frame

    frame属性代表的是当前视图的的位置和大小,没有设置,当前视图你是无法看见的,因此在在初始化视图时要指定视图的frame:

-(id)initWithFrame:(CGRect)frame;
  • frame中的位置是以父视图的坐标系为标准来确定当前视图的位置
  • 修改frame的位置,当前视图的位置也会跟着改变
  • 修改frame的大小,当前视图的大小也会随之改变

center

    center就是视图中心点在父视图中的位置坐标

bounds

      bounds指的是视图在自己坐标系中的位置与大小

  • 更改bounds中位置对当前视图没有影响,但会改变当前视图坐标系的原点位置,相对于子视图来说,当前视图的原点已经不是(0,0)了,而是修改以后的值
UIView *test_View = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 100, 100)];
    test_View.backgroundColor = [UIColor redColor];
    [self.view addSubview:test_View];
    
    UIView *small_View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    small_View.backgroundColor = [UIColor orangeColor];
    [test_View addSubview:small_View];
    
    CGRect bounds = test_View.bounds;
    bounds.origin.x = -20;
    test_View.bounds = bounds;

总结

当View的frame修改时

    1.更改origin,对bounds没有影响,View的center会被改变

    2.更改size,bounds的width和height会跟修改后的frame.size一样,View的center会被修改

当View的bounds修改时

    1.更改origin,frame不会改变,View的center不会改变

    2.更改size  frame.size会与修改后的bounds.size一样,View的center不会改变



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值