一、首先在UIViewController的view上添加一個(gè)橙色窗口。如下圖所示。
UIView * view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 200, 200);
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];

打印出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)系

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);

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);

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];

由此可以看出,如果修改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)!

然后改變view的frame大小位置
view.frame = CGRectMake(200, 400, 100, 100);

因?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);

這里打印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。