iOS中frame和bounds區(qū)別

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容