關(guān)于iOS的布局主要有兩種方式,分別是AutoResizing和AutoLayout。其中AutoResizing作為一種舊的布局方式,在AutoLayout被推廣之后已經(jīng)很少被使用。為了更加清晰的了解iOS的布局方式,本篇針對(duì)于這兩種布局方法進(jìn)行簡(jiǎn)要的總結(jié)。
一.AutoResizing
我們?cè)谑褂肁utoResizing進(jìn)行布局的時(shí)候,其主要思想就是設(shè)置子視圖跟隨父視圖的frame變化而變化。具體的情況,我們可以設(shè)置左跟隨,右跟隨等等。下面是AutoResizing在代碼中的使用。
//父視圖
UIView *superView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
superView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:superView];
//子視圖
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
subView.backgroundColor = [UIColor purpleColor];
[superView addSubview:subView];
//設(shè)置子視圖的寬度隨著父視圖變化
subView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//修改父視圖的frame
superView.frame = CGRectMake(0, 0,200 , 200);
以上代碼中我們?cè)O(shè)置了子視圖的寬度隨父視圖的變化而改變,其效果圖如下:

我們可以看到,圖中的子視圖的寬度也隨著父視圖的寬度增加到了二倍。這就是AutoResizing的一個(gè)最簡(jiǎn)單的應(yīng)用。在我們?cè)趯?shí)際使用時(shí),還有很多的相關(guān)屬性可以設(shè)置。有關(guān)AutoResizing可設(shè)置的布局屬性如下:
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
如果我們需要在XIB中使用AutoResizing,我們需要進(jìn)行如下的操作,在當(dāng)我們創(chuàng)建xib視圖的時(shí)候,我們?cè)赬IB里選中一個(gè)UIView,
1.進(jìn)入file Inspector, 在這里取消AutoLayout屬性,這樣我們就可以使用AutoResizing方法布局了。

2.然后我們進(jìn)入size Inspector,在這里我們可以看到與AutoResizing布局屬性相關(guān)的紅色線條,我們?cè)谶@里選擇或者取消紅線,就相當(dāng)于增加或者取消了子視圖的自動(dòng)跟隨約束。

二、AutoLayout
AutoLayout相比AutoResizing更加實(shí)用,是可以完全替代AutoResizing的一種自動(dòng)布局方式。而在使用AutoLayout前,我們必須理解一個(gè)屬性,那就是translatesAutoresizingMaskIntoConstraints。該屬性表示autoresizingMask和autolayout兩種方式的轉(zhuǎn)換。這個(gè)屬性對(duì)于在代碼中生成的view來(lái)說(shuō)默認(rèn)是true,而對(duì)于IB中拖出來(lái)的view來(lái)說(shuō)默認(rèn)是false.
對(duì)于這一屬性,官方文檔給出的解釋是這樣的:
/* By default, the autoresizing mask on a view gives rise to constraints that fully determine
the view's position. This allows the auto layout system to track the frames of views whose
layout is controlled manually (through -setFrame:, for example).
When you elect to position the view using auto layout by adding your own constraints,
you must set this property to NO. IB will do this for you.
*/
從以上的描述中,我們可以知道在使用AutoResizing布局時(shí),AutoLayout會(huì)根據(jù)autoResizing來(lái)創(chuàng)建同等行為的constraint出來(lái)確定視圖的位置。從而實(shí)現(xiàn)了視圖的自動(dòng)布局。而當(dāng)我們確定選擇使用AutoLayout添加自己的約束的時(shí)候,我們必須設(shè)置此屬性為NO,XIB中這個(gè)屬性默認(rèn)是NO。
在實(shí)際的使用過(guò)程中,我還需要注意兩點(diǎn):
1.當(dāng)我們?cè)O(shè)置這個(gè)屬性為YES的時(shí)候,view的布局結(jié)果由AutoResizingMask,frame,center這些因素共同決定,如果再在其上添加AutoLayout約束,自定義的AutoLayout約束就會(huì)和AutoResizing里Autolayout約束沖突而報(bào)錯(cuò)。
2.我們?cè)O(shè)置該屬性為NO,AutoResizing并不會(huì)直接失效,只有當(dāng)我們?yōu)橐晥D設(shè)置了constraint之后,AutoResizing才會(huì)失效。
那么AutoLayout在開(kāi)發(fā)中具體如何使用呢,這其實(shí)分為兩種情況,一種是借助xib中的約束功能通過(guò)連線的方法實(shí)現(xiàn)。還有一種就是代碼直接添加約束,但是代碼自動(dòng)布局是一件很麻煩的事,我們通常又會(huì)借助第三方即Masonry,下章節(jié)將會(huì)簡(jiǎn)要介紹Masonry的用法。