frame指的是:該view在父view坐標(biāo)系統(tǒng)中的位置和大小。(參照點(diǎn)是父親的坐標(biāo)系統(tǒng))
bounds指的是:該view在本身坐標(biāo)系統(tǒng)中 的位置和大小。(參照點(diǎn)是本身坐標(biāo)系統(tǒng))
以下是代碼,方便理解
UIView *superView = [[UIView alloc]initWithFrame:CGRectMake(20,20,200,200)];
superView.backgroundColor = [UIColor redColor];
[self.view addSubview:superView];
NSLog(@"superView frame:%@========superView bounds:%@",NSStringFromCGRect(superView.frame),NSStringFromCGRect(superView.bounds));
UIView*subView = [[UIView alloc]initWithFrame:CGRectMake(0,0,100,100)];
subView.backgroundColor = [UIColor orangeColor];
[superView addSubview:subView];
NSLog(@"subView frame:%@========subView bounds:%@",NSStringFromCGRect(subView.frame),NSStringFromCGRect(subView.bounds));
控制臺(tái)打印結(jié)果:
superView frame:{{20, 20}, {200, 200}}========superView bounds:{{0, 0}, {200, 200}}
subView frame:{{0, 0}, {100, 100}}========subView bounds:{{0, 0}, {100, 100}}
運(yùn)行結(jié)果如圖一
image
下面我們改變superView的bounds來看看子視圖會(huì)發(fā)生什么變化。
我們?cè)谏厦娑xsuperView的時(shí)候,修改superView的bounds。
[superView setBounds:CGRectMake(-20, -20,200,200)];
控制臺(tái)打印結(jié)果:
superView frame:{{20, 20}, {200, 200}}========superView bounds:{{-20, -20}, {200, 200}}
subView frame:{{0, 0}, {100, 100}}========subView bounds:{{0, 0}, {100, 100}}
運(yùn)行結(jié)果如圖二
image
我們?cè)诟淖兏敢晥D的bounds的時(shí)候,子視圖必將受影響。因?yàn)樽右晥D的frame是根據(jù)父視圖的bounds來確定的。
希望能幫助到你。
Best regards
Roger