iOS基礎(chǔ)---UIView的frame和bounds屬性

一、首先在UIViewController的view上添加一個(gè)橙色窗口。如下圖所示。

    UIView * view = [[UIView alloc] init];
    view.frame = CGRectMake(100, 100, 200, 200);
    view.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:view];
Simulator Screen Shot - iPhone 8 - 2021-11-17 at 10.34.46.png

打印出frame和bounds的值如下:
po view.frame
(origin = (x = 100, y = 100), size = (width = 200, height = 200))
po view.bounds
(origin = (x = 0, y = 0), size = (width = 200, height = 200))
網(wǎng)上也有個(gè)圖很好的說(shuō)明了兩者之間的關(guān)系


1364058232_8785.jpg

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),就相當(dāng)于ViewB自己的坐標(biāo)系統(tǒng),以0,0點(diǎn)為起點(diǎn))
center:該view的中心點(diǎn)在父view坐標(biāo)系統(tǒng)中的位置和大小。(參照點(diǎn)是父親的坐標(biāo)系統(tǒng))

二、修改view的frame

  view.frame = CGRectInset(view.frame, 50, 50);
Simulator Screen Shot - iPhone 8 - 2021-11-17 at 11.16.31.png

po view.frame
(origin = (x = 150, y = 150), size = (width = 100, height = 100))
po view.bounds
(origin = (x = 0, y = 0), size = (width = 100, height = 100))
可以看到CGRectInset使x方向和y方向的2邊都減少了50個(gè)點(diǎn)。這個(gè)結(jié)果我們也比較容易理解。

三,修改view的bounds

    view.bounds = CGRectInset(view.bounds, 50, 50);
Simulator Screen Shot - iPhone 8 - 2021-11-17 at 11.16.31.png

po view.frame
(origin = (x = 150, y = 150), size = (width = 100, height = 100))
po view.bounds
(origin = (x = 50, y = 50), size = (width = 100, height = 100))
這樣修改后,view的frame跟上面一樣。但bound的origin卻不再是(0,0)了。這代表什么意思呢?

四,再給view添加一個(gè)綠色的子view

    UIView * view = [[UIView alloc] init];
    view.frame = CGRectMake(100, 100, 200, 200);
    view.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:view];

    view.bounds = CGRectInset(view.bounds, 50, 50);

    UIView * subView =  [[UIView alloc] init];
    subView.frame = CGRectMake(0, 0, 30, 30);
    subView.backgroundColor = [UIColor greenColor];
    [view addSubview:subView];
Simulator Screen Shot - iPhone 8 - 2021-11-17 at 11.58.31.png

由此可以看出,如果修改view的bounds,會(huì)對(duì)添加到其上的子subview的位置產(chǎn)生影響。view的bounds.origin修改了,則其左上角不再是(0,0),這里變成(50,50),如果subview的frame是(0, 0, 30, 30);則其會(huì)向左上角偏移50個(gè)點(diǎn)。

五、綜合幾個(gè)例子看看

1.給橙色的view添加一個(gè)綠色的subView

    UIView * view = [[UIView alloc] init];
    view.frame = CGRectMake(100, 100, 200, 200);
    view.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:view];
    
    UIView * subView =  [[UIView alloc] init];
    subView.frame = CGRectMake(0, 0, 30, 30);
    subView.backgroundColor = [UIColor greenColor];
    [view addSubview:subView];

打印一下可以知道現(xiàn)在的view.center是(200,200)!


Simulator Screen Shot - iPhone 8 - 2021-11-17 at 16.05.17.png

然后改變view的frame大小位置

    view.frame =  CGRectMake(200, 400, 100, 100);
Simulator Screen Shot - iPhone 8 - 2021-11-17 at 16.05.57.png

因?yàn)閒rame是相對(duì)于父窗口的,可以看到view的大小和位置都發(fā)生了改變。相對(duì)父窗口的位置變?yōu)?200,400),大小變?yōu)?100,100)。
此時(shí)的view.center是(250,450)

下面不改變view的frame,改變view的bounds:

    view.bounds = CGRectMake(100, 50, 100, 100);
Simulator Screen Shot - iPhone 8 - 2021-11-17 at 17.30.27.png

這里打印view.center,依然是(200,200).所以改變view.bounds
不會(huì)改變view.center。只是改變了view.bounds.origin(現(xiàn)在是(100,50))的和view.bounds.size(現(xiàn)在是(100,100))。如果添加一個(gè)subView其frame(0,0,30,30)則其會(huì)向上偏移50,向左偏移100??梢钥吹絪ubView.frame,是基于父窗口的bounds。

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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