IOS-UI 属性frame和bounds

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

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

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

下面看例子class

@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.000object

frame-width:100.000000, frame-height:100.0im

bounds-x:0.00, bounds-y:0.00view

bounds-width:100.00, bounds-height:100.00vi

相关文章
相关标签/搜索