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