IOS-UI 属性frame和bounds

在使用代码编写界面的时候会遇到一些重要属性,其中frame和bounds这两个属性可能比较容易混淆

bounds:表示该图示在本地坐标系统中的位置和大小,相对于自己

frame:表示view在其父视图坐标系统中的位置和大小,相对于父视图

下面看例子

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    UIView *viewA = [[UIView alloc] init];
    viewA.backgroundColor = [UIColor grayColor];
    //设置viewA的frame属性
    viewA.frame = CGRectMake(0, 0, 300, 500);
    [self.view addSubview:viewA];
    
    
    
    UIView *viewB = [[UIView alloc] init];
    viewB.backgroundColor = UIColor.orangeColor;
    viewB.frame = CGRectMake(20, 80, 100, 100);
    [self.view addSubview:viewB];
    
    NSLog(@"frame-x:%0.2f, frame-y:%.3f", viewB.frame.origin.x, viewB.frame.origin.y);
    NSLog(@"frame-width:%f, frame-height:%.1f", viewB.frame.size.width, viewB.frame.size.height);
    NSLog(@"bounds-x:%0.2f, bounds-y:%.2f", viewB.bounds.origin.x, viewB.bounds.origin.y);
    NSLog(@"bounds-width:%0.2f, bounds-height:%.2f", viewB.bounds.size.width, viewB.bounds.size.height);
    
//    2018-06-28 14:12:22.333305+0800 UIViewDemo[79588:3484414] frame-x:20.00, frame-y:80.000
//    2018-06-28 14:12:22.333494+0800 UIViewDemo[79588:3484414] frame-width:100.000000, frame-height:100.0
//    2018-06-28 14:12:22.333595+0800 UIViewDemo[79588:3484414] bounds-x:0.00, bounds-y:0.00
//    2018-06-28 14:12:22.333730+0800 UIViewDemo[79588:3484414] bounds-width:100.00, bounds-height:100.00
//    上面结果还体现了占位符问题,%f 默认取小数点后6位,%f.2表示取小数点后2位,%f0.2小国和%f.2一样,%f.1 取小数点后一位


@end

frame-x:20.00, frame-y:80.000

frame-width:100.000000, frame-height:100.0

bounds-x:0.00, bounds-y:0.00

bounds-width:100.00, bounds-height:100.00

转载于:https://my.oschina.net/goboy/blog/1836951

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值