UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 100)];
view1.backgroundColor = [UIColor blueColor];
view1.bounds = CGRectMake(0, 100, 100, 100);
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 110, 50, 50)];
view2.bounds = CGRectMake(0, 0, 50, 50);
// view2.bounds = CGRectMake(0, 50, 50, 20);
view2.backgroundColor = [UIColor redColor];
[view1 addSubview:view2];
NSLog(@"bounds={%@} center={%@}",NSStringFromCGPoint(view1.bounds.origin),NSStringFromCGPoint(view1.frame.origin));
NSLog(@"bounds={%@} center={%@}",NSStringFromCGPoint(view2.bounds.origin),NSStringFromCGPoint(view2.frame.origin));
蓝色为A视图,红色为B视图,A视图的bounds为 B加入到A Abounds为0,100, B设置frame时,如果要从A的原点开始设置就要设置为0,100 如果要设置相对A视图以下10个像素,就要设置为A的bounds 加+相对坐标。
这样理解起来就容易多了。总结一句就是,bounds 是自己的坐标系,要在这个坐标系上加入子view 就要设置相对坐标,这个相对坐标是更根据父视图的bounds 坐标来的,初始化时bounds都为0 所以我们就可以用普通的相对值来设置frame。