ios視圖frame和bounds的對比

bounds座標:本身定義的座標系統,setbound指明瞭本視圖左上角在該座標系統中的座標,spa

        默認值(0,0)code

frame座標:  子視圖左上角在父視圖座標系統(bounds座標系統)中的座標,默認值(0,0)blog

子視圖實際位置=父視圖實際位置-父視圖bounds座標+子視圖frame座標it

1、boundsio

  隻影響「子視圖」相對屏幕的位置,修改時不會影響自身相對屏幕的位置class

  一、父視圖bounds座標爲(0,0)時im

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));
}

  

  二、父視圖bounds座標爲(-20,-20)時 call

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    [self.view setBounds:CGRectMake(-20, -20, 320, 568)];
    
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));
}

  

2、frameimg

  修改時改變了本身的在父視圖座標系統(bounds座標系統)的位置,自身位置和di

  子視圖位置都會被改變。

  一、父視圖frame座標(0,0)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    view2.backgroundColor = [UIColor yellowColor];
    [view1 addSubview:view2];
    
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds));
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));
}

  

  二、父視圖frame座標(60,60)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(60, 60, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    view2.backgroundColor = [UIColor yellowColor];
    [view1 addSubview:view2];
    
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds));
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));
}

  

3、說明

  根視圖只能修改bounds座標,而不能夠修改frame座標

  如下self.view爲根視圖

  一、初始狀態

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
}

  

  二、修改bounds座標:有效

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    CGRect viewBounds = self.view.bounds;
    viewBounds.origin.y=-40;
    viewBounds.origin.x=-40;
    self.view.bounds=viewBounds;
    
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));
}

   

   

  三、修改frame座標:無效

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 260)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y=60;
    viewFrame.origin.x=60;
    self.view.frame=viewFrame;
    
    NSLog(@"view frame:%@========view bounds:%@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.bounds));
}

  

  

相關文章
相關標籤/搜索