因?yàn)橹伴_發(fā)時(shí)都是在xib文件中添加約束,或者代碼中計(jì)算frame并沒有接觸過Masonry,現(xiàn)在寫篇博客來歸納總結(jié)下Masonry的使用和注意點(diǎn)撩炊。這篇文章只是簡(jiǎn)單介紹Masonry放仗,以及Masonry的使用,并且會(huì)舉一些例子出來包警。但并不會(huì)涉及到Masonry的內(nèi)部實(shí)現(xiàn)
Masonry中的坑:
在使用Masonry進(jìn)行約束時(shí)撵摆,有一些是需要注意的。
在使用Masonry添加約束之前害晦,需要在addSubview之后才能使用特铝,否則會(huì)導(dǎo)致崩潰。
在添加約束時(shí)初學(xué)者經(jīng)常會(huì)出現(xiàn)一些錯(cuò)誤壹瘟,約束出現(xiàn)問題的原因一般就是兩種:約束沖突和缺少約束鲫剿。對(duì)于這兩種問題,可以通過調(diào)試和log排查稻轨。
之前使用Interface Builder添加約束灵莲,如果約束有錯(cuò)誤直接就可以看出來,并且會(huì)以紅色或者黃色警告體現(xiàn)出來殴俱。而Masonry則不會(huì)直觀的體現(xiàn)出來政冻,而是以運(yùn)行過程中崩潰或者打印異常log體現(xiàn)枚抵,所以這也是手寫代碼進(jìn)行AutoLayout的一個(gè)缺點(diǎn)。
這個(gè)問題只能通過多敲代碼明场,積攢純代碼進(jìn)行AutoLayout的經(jīng)驗(yàn)俄精,慢慢就用起來越來越得心應(yīng)手了。
Masonry基礎(chǔ)使用
Masonry基礎(chǔ)API
mas_makeConstraints() 添加約束.
mas_remakeConstraints() 移除之前的約束榕堰,重新添加新的約束.
mas_updateConstraints() 更新約束
equalTo() 參數(shù)是對(duì)象類型竖慧,一般是視圖對(duì)象或者mas_width這樣的坐標(biāo)系對(duì)象
mas_equalTo() 和上面功能相同,參數(shù)可以傳遞基礎(chǔ)數(shù)據(jù)類型對(duì)象逆屡,可以理解為比上面的API更強(qiáng)大
width() 用來表示寬度圾旨,例如代表view的寬度
mas_width() 用來獲取寬度的值。和上面的區(qū)別在于魏蔗,一個(gè)代表某個(gè)坐標(biāo)系對(duì)象砍的,一個(gè)用來獲取坐標(biāo)系對(duì)象的值
Auto Boxing
上面例如equalTo或者width這樣的,有時(shí)候需要涉及到使用mas_前綴莺治,這在開發(fā)中需要注意作區(qū)分廓鞠。
如果在當(dāng)前類引入#import "Masonry.h"之前,用下面兩種宏定義聲明一下谣旁,就不需要區(qū)分mas_前綴床佳。
// 定義這個(gè)常量,就可以不用在開發(fā)過程中使用"mas_"前綴榄审。
define MAS_SHORTHAND
// 定義這個(gè)常量砌们,就可以讓Masonry幫我們自動(dòng)把基礎(chǔ)數(shù)據(jù)類型的數(shù)據(jù),自動(dòng)裝箱為對(duì)象類型搁进。
define MAS_SHORTHAND_GLOBALS
修飾語句
Masonry為了讓代碼使用和閱讀更容易理解浪感,所以直接通過點(diǎn)語法就可以調(diào)用,還添加了and和with兩個(gè)方法饼问。這兩個(gè)方法內(nèi)部實(shí)際上什么都沒干影兽,只是在內(nèi)部將self直接返回,功能就是為了更加方便閱讀莱革,對(duì)代碼執(zhí)行沒有實(shí)際作用峻堰。
例如下面的例子:
make.top.and.bottom.equalTo(self.containerView).with.offset(padding);
其內(nèi)部代碼實(shí)現(xiàn),實(shí)際上就是直接將self返回驮吱。
- (MASConstraint *)with {
return self;
}
更新約束和布局
關(guān)于更新約束布局相關(guān)的API茧妒,主要用以下四個(gè)API:
- (void)updateConstraintsIfNeeded 調(diào)用此方法,如果有標(biāo)記為需要重新布局的約束左冬,則立即進(jìn)行重新布局桐筏,內(nèi)部會(huì)調(diào)用updateConstraints方法
- (void)updateConstraints 重寫此方法,內(nèi)部實(shí)現(xiàn)自定義布局過程
- (BOOL)needsUpdateConstraints 當(dāng)前是否需要重新布局拇砰,內(nèi)部會(huì)判斷當(dāng)前有沒有被標(biāo)記的約束
- (void)setNeedsUpdateConstraints 標(biāo)記需要進(jìn)行重新布局
關(guān)于UIView重新布局相關(guān)的API梅忌,主要用以下三個(gè)API:
- (void)setNeedsLayout 標(biāo)記為需要重新布局
- (void)layoutIfNeeded 查看當(dāng)前視圖是否被標(biāo)記需要重新布局狰腌,有則在內(nèi)部調(diào)用layoutSubviews方法進(jìn)行重新布局
- (void)layoutSubviews 重寫當(dāng)前方法,在內(nèi)部完成重新布局操作
Masonry示例代碼
Masonry本質(zhì)上就是對(duì)系統(tǒng)AutoLayout進(jìn)行的封裝牧氮,包括里面很多的API琼腔,都是對(duì)系統(tǒng)API進(jìn)行了一次二次包裝。
typedef NS_OPTIONS(NSInteger, MASAttribute) {
MASAttributeLeft = 1 << NSLayoutAttributeLeft,
MASAttributeRight = 1 << NSLayoutAttributeRight,
MASAttributeTop = 1 << NSLayoutAttributeTop,
MASAttributeBottom = 1 << NSLayoutAttributeBottom,
MASAttributeLeading = 1 << NSLayoutAttributeLeading,
MASAttributeTrailing = 1 << NSLayoutAttributeTrailing,
MASAttributeWidth = 1 << NSLayoutAttributeWidth,
MASAttributeHeight = 1 << NSLayoutAttributeHeight,
MASAttributeCenterX = 1 << NSLayoutAttributeCenterX,
MASAttributeCenterY = 1 << NSLayoutAttributeCenterY,
MASAttributeBaseline = 1 << NSLayoutAttributeBaseline,
};
常用方法
設(shè)置內(nèi)邊距
/**
設(shè)置yellow視圖和self.view等大踱葛,并且有10的內(nèi)邊距丹莲。
注意根據(jù)UIView的坐標(biāo)系,下面right和bottom進(jìn)行了取反尸诽。所以不能寫成下面這樣甥材,否則right、bottom這兩個(gè)方向會(huì)出現(xiàn)問題性含。
make.edges.equalTo(self.view).with.offset(10);
除了下面例子中的offset()方法洲赵,還有針對(duì)不同坐標(biāo)系的centerOffset()、sizeOffset()商蕴、valueOffset()之類的方法叠萍。
*/
[self.yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).with.offset(10);
make.top.equalTo(self.view).with.offset(10);
make.right.equalTo(self.view).with.offset(-10);
make.bottom.equalTo(self.view).with.offset(-10);
}];
通過insets簡(jiǎn)化設(shè)置內(nèi)邊距的方式
// 下面的方法和上面例子等價(jià),區(qū)別在于使用insets()方法绪商。
[self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// 下苛谷、右不需要寫負(fù)號(hào),insets方法中已經(jīng)為我們做了取反的操作了部宿。
make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
}];
更新約束
// 設(shè)置greenView的center和size抄腔,這樣就可以達(dá)到簡(jiǎn)單進(jìn)行約束的目的
[self.greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
// 這里通過mas_equalTo給size設(shè)置了基礎(chǔ)數(shù)據(jù)類型的參數(shù),參數(shù)為CGSize的結(jié)構(gòu)體
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
// 為了更清楚的看出約束變化的效果理张,在顯示兩秒后更新約束。
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.greenView mas_updateConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view).offset(100);
make.size.mas_equalTo(CGSizeMake(100, 100));
}];
});
大于等于和小于等于某個(gè)值的約束
[self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
// 設(shè)置寬度小于等于200
make.width.lessThanOrEqualTo(@200);
// 設(shè)置高度大于等于10
make.height.greaterThanOrEqualTo(@(10));
}];
self.textLabel.text = @"這是測(cè)試的字符串绵患。能看到1雾叭、2、3個(gè)步驟落蝙,第一步當(dāng)然是上傳照片了织狐,要上傳正面近照哦。上傳后筏勒,網(wǎng)站會(huì)自動(dòng)識(shí)別你的面部移迫,如果覺得識(shí)別的不準(zhǔn),你還可以手動(dòng)修改一下管行。左邊可以看到16項(xiàng)修改參數(shù)厨埋,最上面是整體修改,你也可以根據(jù)自己的意愿單獨(dú)修改某項(xiàng)捐顷,將鼠標(biāo)放到選項(xiàng)上面荡陷,右邊的預(yù)覽圖會(huì)顯示相應(yīng)的位置雨效。";
textLabel只需要設(shè)置一個(gè)屬性即可
self.textLabel.numberOfLines = 0;
使用基礎(chǔ)數(shù)據(jù)類型當(dāng)做參數(shù)
/**
如果想使用基礎(chǔ)數(shù)據(jù)類型當(dāng)做參數(shù),Masonry為我們提供了"mas_xx"格式的宏定義废赞。
這些宏定義會(huì)將傳入的基礎(chǔ)數(shù)據(jù)類型轉(zhuǎn)換為NSNumber類型徽龟,這個(gè)過程叫做封箱(Auto Boxing)。
"mas_xx"開頭的宏定義唉地,內(nèi)部都是通過MASBoxValue()函數(shù)實(shí)現(xiàn)的据悔。
這樣的宏定義主要有四個(gè),分別是mas_equalTo()耘沼、mas_offset()和大于等于屠尊、小于等于四個(gè)。
*/
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.width.mas_equalTo(100);
make.height.mas_equalTo(100);
}];
設(shè)置約束優(yōu)先級(jí)
/**
Masonry為我們提供了三個(gè)默認(rèn)的方法耕拷,priorityLow()讼昆、priorityMedium()、priorityHigh()骚烧,這三個(gè)方法內(nèi)部對(duì)應(yīng)著不同的默認(rèn)優(yōu)先級(jí)浸赫。
除了這三個(gè)方法,我們也可以自己設(shè)置優(yōu)先級(jí)的值赃绊,可以通過priority()方法來設(shè)置既峡。
*/
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.width.equalTo(self.view).priorityLow();
make.width.mas_equalTo(20).priorityHigh();
make.height.equalTo(self.view).priority(200);
make.height.mas_equalTo(100).priority(1000);
}];
Masonry也幫我們定義好了一些默認(rèn)的優(yōu)先級(jí)常量,分別對(duì)應(yīng)著不同的數(shù)值碧查,優(yōu)先級(jí)最大數(shù)值是1000运敢。
static const MASLayoutPriority MASLayoutPriorityRequired = UILayoutPriorityRequired;
static const MASLayoutPriority MASLayoutPriorityDefaultHigh = UILayoutPriorityDefaultHigh;
static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 500;
static const MASLayoutPriority MASLayoutPriorityDefaultLow = UILayoutPriorityDefaultLow;
static const MASLayoutPriority MASLayoutPriorityFittingSizeLevel = UILayoutPriorityFittingSizeLevel;
設(shè)置約束比例
// 設(shè)置當(dāng)前約束值乘以多少,例如這個(gè)例子是redView的寬度是self.view寬度的0.2倍忠售。
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.height.mas_equalTo(30);
make.width.equalTo(self.view).multipliedBy(0.2);
}];
小練習(xí)
子視圖等高練習(xí)
/**
下面的例子是通過給equalTo()方法傳入一個(gè)數(shù)組传惠,設(shè)置數(shù)組中子視圖及當(dāng)前make對(duì)應(yīng)的視圖之間等高。
需要注意的是稻扬,下面block中設(shè)置邊距的時(shí)候卦方,應(yīng)該用insets來設(shè)置,而不是用offset泰佳。
因?yàn)橛胦ffset設(shè)置right和bottom的邊距時(shí)盼砍,這兩個(gè)值應(yīng)該是負(fù)數(shù),所以如果通過offset來統(tǒng)一設(shè)置值會(huì)有問題逝她。
*/
CGFloat padding = LXZViewPadding;
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.equalTo(self.view).insets(UIEdgeInsetsMake(padding, padding, 0, padding));
make.bottom.equalTo(self.blueView.mas_top).offset(-padding);
}];
[self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view).insets(UIEdgeInsetsMake(0, padding, 0, padding));
make.bottom.equalTo(self.yellowView.mas_top).offset(-padding);
}];
/**
下面設(shè)置make.height的數(shù)組是關(guān)鍵浇坐,通過這個(gè)數(shù)組可以設(shè)置這三個(gè)視圖高度相等。其他例如寬度之類的黔宛,也是類似的方式近刘。
*/
[self.yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(self.view).insets(UIEdgeInsetsMake(0, padding, padding, padding));
make.height.equalTo(@[self.blueView, self.redView]);
}];
子視圖垂直居中練習(xí)
/**
要求:(這個(gè)例子是在其他人博客里看到的,然后按照要求自己寫了下面這段代碼)
兩個(gè)視圖相對(duì)于父視圖垂直居中,并且兩個(gè)視圖以及父視圖之間的邊距均為10跌宛,高度為150酗宋,兩個(gè)視圖寬度相等。
*/
CGFloat padding = 10.f;
[self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.view);
make.left.equalTo(self.view).mas_offset(padding);
make.right.equalTo(self.redView.mas_left).mas_offset(-padding);
make.width.equalTo(self.redView);
make.height.mas_equalTo(150);
}];
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.view);
make.right.equalTo(self.view).mas_offset(-padding);
make.width.equalTo(self.blueView);
make.height.mas_equalTo(150);
}];
UITableView動(dòng)態(tài)Cell高度
在iOS UI開發(fā)過程中疆拘,UITableView的動(dòng)態(tài)Cell高度一直都是個(gè)問題蜕猫。實(shí)現(xiàn)這樣的需求,實(shí)現(xiàn)方式有很多種哎迄,只是實(shí)現(xiàn)起來復(fù)雜程度和性能的區(qū)別回右。
在不考慮性能的情況下,tableView動(dòng)態(tài)Cell高度漱挚,可以采取估算高度的方式翔烁。如果通過估算高度的方式實(shí)現(xiàn)的話,無論是純代碼還是Interface Builder旨涝,都只需要兩行代碼就可以完成Cell自動(dòng)高度適配蹬屹。
實(shí)現(xiàn)方式:
需要設(shè)置tableView的rowHeight屬性,這里設(shè)置為自動(dòng)高度白华,告訴系統(tǒng)Cell的高度是不固定的慨默,需要系統(tǒng)幫我們進(jìn)行計(jì)算。然后設(shè)置tableView的estimatedRowHeight屬性弧腥,設(shè)置一個(gè)估計(jì)的高度厦取。(我這里用的代理方法,實(shí)際上都一樣)
原理:
這樣的話管搪,在tableView被創(chuàng)建之后虾攻,系統(tǒng)會(huì)根據(jù)estimatedRowHeight屬性設(shè)置的值,為tableView設(shè)置一個(gè)估計(jì)的值更鲁。然后在Cell顯示的時(shí)候再獲取Cell的高度霎箍,并刷新tableView的contentSize。
(void)tableViewConstraints {
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
}(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataList.count;
}(MasonryTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MasonryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LXZTableViewCellIdentifier];
[cell reloadViewWithText:self.dataList[indexPath.row]];
return cell;
}
// 需要注意的是岁经,這個(gè)代理方法和直接返回當(dāng)前Cell高度的代理方法并不一樣朋沮。
// 這個(gè)代理方法會(huì)將當(dāng)前所有Cell的高度都預(yù)估出來,而不是只計(jì)算顯示的Cell缀壤,所以這種方式對(duì)性能消耗還是很大的。
// 所以通過設(shè)置estimatedRowHeight屬性的方式纠亚,和這種代理方法的方式塘慕,最后性能消耗都是一樣的。
(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50.f;
}(UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
// 設(shè)置tableView自動(dòng)高度
_tableView.rowHeight = UITableViewAutomaticDimension;
[_tableView registerClass:[MasonryTableViewCell class] forCellReuseIdentifier:LXZTableViewCellIdentifier];
[self.view addSubview:_tableView];
}
return _tableView;
}
UIScrollView自動(dòng)布局
之前聽很多人說過UIScrollView很麻煩蒂胞,然而我并沒有感覺到有多麻煩(并非裝逼)图呢。我感覺說麻煩的人可能根本就沒試過吧,只是覺得很麻煩而已。
我這里就講一下兩種進(jìn)行UIScrollView自動(dòng)布局的方案蛤织,并且會(huì)講一下自動(dòng)布局的技巧赴叹,只要掌握技巧,布局其實(shí)很簡(jiǎn)單指蚜。
布局小技巧:
給UIScrollView添加的約束是定義其frame乞巧,設(shè)置contentSize是定義其內(nèi)部大小。UIScrollView進(jìn)行addSubview操作摊鸡,都是將其子視圖添加到contentView上绽媒。
所以,添加到UIScrollView上的子視圖免猾,對(duì)UIScrollView添加的約束都是作用于contentView上的是辕。只需要按照這樣的思路給UIScrollView設(shè)置約束,就可以掌握設(shè)置約束的技巧了猎提。
提前設(shè)置contentSize
// 提前設(shè)置好UIScrollView的contentSize获三,并設(shè)置UIScrollView自身的約束
self.scrollView.contentSize = CGSizeMake(1000, 1000);
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
// 雖然redView的get方法內(nèi)部已經(jīng)執(zhí)行過addSubview操作,但是UIView始終以最后一次添加的父視圖為準(zhǔn)锨苏,也就是redView始終是在最后一次添加的父視圖上疙教。
[self.scrollView addSubview:self.redView];
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(self.scrollView);
make.width.height.mas_equalTo(200);
}];
[self.scrollView addSubview:self.blueView];
[self.blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.redView.mas_right);
make.top.equalTo(self.scrollView);
make.width.height.equalTo(self.redView);
}];
[self.scrollView addSubview:self.greenView];
[self.greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.scrollView);
make.top.equalTo(self.redView.mas_bottom);
make.width.height.equalTo(self.redView);
}];
自動(dòng)contentSize
上面的例子是提前設(shè)置好UIScrollView的contentSize的內(nèi)部size,然后直接向里面addSubview蚓炬。但是這有個(gè)要求就是松逊,需要提前知道contentSize的大小,不然沒法設(shè)置肯夏。
這個(gè)例子中將會(huì)展示動(dòng)態(tài)改變contentSize的大小经宏,內(nèi)部視圖有多少contentSize就自動(dòng)擴(kuò)充到多大。
這種方式的實(shí)現(xiàn)驯击,主要是依賴于創(chuàng)建一個(gè)containerView內(nèi)容視圖烁兰,并添加到UIScrollView上作為子視圖。UIScrollView原來的子視圖都添加到containerView上徊都,并且和這個(gè)視圖設(shè)置約束沪斟。
因?yàn)閷?duì)UIScrollView進(jìn)行addSubview操作的時(shí)候,本質(zhì)上是往其contentView上添加暇矫。也就是containerView的父視圖是contentView主之,通過containerView撐起contentView視圖的大小,以此來實(shí)現(xiàn)動(dòng)態(tài)改變contentSize李根。
// 在進(jìn)行約束的時(shí)候槽奕,要對(duì)containerView的上下左右都添加和子視圖的約束,以便確認(rèn)containerView的邊界區(qū)域房轿。
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
CGFloat padding = LXZViewPadding;
[self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.scrollView).insets(UIEdgeInsetsMake(padding, padding, padding, padding));
}];
[self.containerView addSubview:self.greenView];
[self.greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.equalTo(self.containerView).offset(padding);
make.size.mas_equalTo(CGSizeMake(250, 250));
}];
[self.containerView addSubview:self.redView];
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.containerView).offset(padding);
make.left.equalTo(self.greenView.mas_right).offset(padding);
make.size.equalTo(self.greenView);
make.right.equalTo(self.containerView).offset(-padding);
}];
[self.containerView addSubview:self.yellowView];
[self.yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.containerView).offset(padding);
make.top.equalTo(self.greenView.mas_bottom).offset(padding);
make.size.equalTo(self.greenView);
make.bottom.equalTo(self.containerView).offset(-padding);
}];
多個(gè)(2個(gè)以上)控件的等間隔排序顯示
首先介紹2個(gè)函數(shù)
/**
* axisType 軸線方向
* fixedSpacing 間隔大小
* fixedItemLength 每個(gè)控件的固定長(zhǎng)度/寬度
* leadSpacing 頭部間隔
* tailSpacing 尾部間隔
*
*/
//1. 等間隔排列 - 多個(gè)控件間隔固定粤攒,控件長(zhǎng)度/寬度變化
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType
withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing
tailSpacing:(CGFloat)tailSpacing;
//2. 等間隔排列 - 多個(gè)固定大小固定所森,間隔空隙變化
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType
withFixedItemLength:(CGFloat)fixedItemLength
leadSpacing:(CGFloat)leadSpacing
tailSpacing:(CGFloat)tailSpacing;
//首先添加5個(gè)視圖
NSMutableArray *array = [NSMutableArray new];
for (int i = 0; i < 5; i ++) {
UIView *view = [UIView new];
view.backgroundColor = [UIColor greenColor];
[self addSubview:view];
[array addObject:view]; //保存添加的控件
}
//水平方向控件間隔固定等間隔
[array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15 leadSpacing:10 tailSpacing:10];
[array makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(50);
make.height.equalTo(70);
}];
//水平方向?qū)挾裙潭ǖ乳g隔
[array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:70 leadSpacing:10 tailSpacing:10];
[array makeConstraints:^(MASConstraintMaker *make) { //數(shù)組額你不必須都是view
make.top.equalTo(50);
make.height.equalTo(70);
}];