bounds是指这个view在它自己坐标系的坐标和大小 而frame指的是这个view在它superview的坐标系的坐标和大小
区别主要在坐标系这一块。
很明显一个是自己为原点的坐标系,一个是以屏幕为原点的坐标系。绝对坐标。。。相对坐标。。。比如屏幕旋转的时候就要以相对来重绘。
frame 如果一个按钮,是在表格里,按钮的frame 的坐标也是相对的,并不是相对屏幕,也就是说是相对坐标,不是绝对坐标
我也想知道任何一个uiview如何求得它在屏幕上的坐标。
view 的frame是view在它的super view 的位置与尺寸。
view 的bounds可以用来帮助它的subview来定位的 ,layoutSubviews。
Frame is in terms of superview's coordinate system
框架是从父视图的坐标系统
Bounds is in terms of local coordinate system
是在局部坐标系统。
frame和bounds是UIView中的两个属性(property)。
frame指的是:该view在父view坐标系统中的位置和大小。(参照点是父亲的坐标系统)
bounds指的是:该view在本身坐标系统中 的位置和大小。(参照点是本身坐标系统)
ios视图frame和bounds的区别:
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));
}