(0046) iOS开发之View的frame和bounds之解惑
iOS view的frame和bounds之解惑
frame: 该view在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统)
bounds:该view在本地坐标系统中的位置和大小。(参照点是,本地坐标系统,就相当于ViewB自己的坐标系统,以0,0点为起点)
center:该view的中心点在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统)
二、demo演示:
- (void)testFrameWithBounds
{
UIView *view1 = [[UIViewalloc] initWithFrame:CGRectMake(20,80, 280,250)];
[view1 setBounds:CGRectMake(-20, -100,280, 250)];
view1.backgroundColor = [UIColorredColor];
[self.viewaddSubview:view1];//添加到self.view
NSLog(@"view1 frame:%@========view1 bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds));
UIView *view2 = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 100,100)];
view2.backgroundColor = [UIColoryellowColor];
[view1 addSubview:view2];//添加到view1上,[此时view1坐标系左上角起点为(-20,-20)]
NSLog(@"view2 frame:%@========view2 bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));
}
// 控制台打印:
$:view1 frame:{{20, 80}, {280, 250}}========view1 bounds:{{-20, -100}, {280, 250}}
$: view2 frame:{{0, 0}, {100, 100}}========view2 bounds:{{0, 0}, {100, 100}}
试验得知结论:
1. bounds 的默认的原点是{0, 0}。
2. bounds 原点是可以修改的。setBounds。{-20, -100},修改的是自己的bounds 的原点。影响的是子视图的位置。