ios視圖frame和bounds的對比

來源:互聯網
上載者:User

bounds座標:自己定義的座標系統,setbound指明了本視圖左上方在該座標系統中的座標,

        預設值(0,0)

frame座標:  子視圖左上方在父視圖座標系統(bounds座標系統)中的座標,預設值(0,0)

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

一、bounds

  隻影響“子視圖”相對螢幕的位置,修改時不會影響自身相對螢幕的位置

  1、父視圖bounds座標為(0,0)時

- (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));}

  

  2、父視圖bounds座標為(-20,-20)時 

- (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));}

  

二、frame

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

  子視圖位置都會被改變。

  1、父視圖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));}

  

  2、父視圖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));}

  

三、說明

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

  以下self.view為根視圖

  1、初始狀態

- (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];}

  

  2、修改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));}

   

   

  3、修改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));}

  

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.