UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 100, 100)];spa
view1.backgroundColor = [UIColor blueColor];3d
view1.bounds = CGRectMake(0, 100, 100, 100);orm
[self.view addSubview:view1];it
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 110, 50, 50)];總結
view2.bounds = CGRectMake(0, 0, 50, 50);margin
// view2.bounds = CGRectMake(0, 50, 50, 20);top
view2.backgroundColor = [UIColor redColor];view
[view1 addSubview:view2];vi
NSLog(@"bounds={%@} center={%@}",NSStringFromCGPoint(view1.bounds.origin),NSStringFromCGPoint(view1.frame.origin));co
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。