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