天天看點

iOS frame、bounds、center

frame:描述目前視圖在其父視圖中的位置和大小。

bounds:描述目前視圖在其自身坐标系統中的位置和大小。

center:描述目前視圖的中心點在其父視圖中的位置。

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *view0 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 120, 120)];
    view0.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view0];
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 120, 120)];
    [view1 setBounds:CGRectMake(20, 0, 10, 120)];
    view1.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:view1];//添加到self.view
    NSLog(@"view1 frame:%@========view1 bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds));
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    view2.backgroundColor = [UIColor yellowColor];
    [view1 addSubview:view2];//添加到view1上,[此時view1坐标系左上角起點為(-20,-20)]
    NSLog(@"view2 frame:%@========view2 bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));

    // Do any additional setup after loading the view, typically from a nib.
}