Masonry

一、幾個(gè)重要的屬性


  • 兩個(gè)屬性Content Compression Resistance(排擠,表示當(dāng)前Label不想被收縮,值越高越不容易收縮).和Content Hugging(擁抱,表示當(dāng)前Label不想被拉伸,值越高越不容易被拉伸);用好這兩個(gè)屬性,可以更加高效的布局。
    HuggingPriority:默認(rèn)值為250;
    CompressionResistancePriority:默認(rèn)值為750;
    兩個(gè)Label并排顯示,寬度根據(jù)內(nèi)容而定。分為兩種情況。1. 左右兩邊數(shù)據(jù)都不足,那個(gè)lable拉伸?這種情況要用setContentHuggingPriority,想讓哪邊的label固定不拉伸,就設(shè)置那個(gè)label的優(yōu)先級(jí)大于250(兩個(gè)label,優(yōu)先級(jí)越高越不容易被拉伸,另一個(gè)label是默認(rèn)值250,所以要把不想拉伸的label優(yōu)先級(jí)設(shè)為大于250)。2.左右兩邊數(shù)據(jù)都才充足的時(shí)候,那個(gè)label收縮?這種情況要用setContentCompressionResistancePriority,想讓哪邊的label固定不會(huì)收縮,就設(shè)置那個(gè)label的優(yōu)先級(jí)大于750(兩個(gè)label,優(yōu)先級(jí)越高越不容易被收縮,另一個(gè)label是默認(rèn)值750,所以要把不想收縮的label優(yōu)先級(jí)設(shè)為大于750);
    Masonry設(shè)置優(yōu)先級(jí)的代碼如下
//content hugging 為1000
[view setContentHuggingPriority:UILayoutPriorityRequired
                           forAxis:UILayoutConstraintAxisHorizontal];

//content compression 為250
[view setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
                                         forAxis:UILayoutConstraintAxisHorizontal];
  • multipler屬性表示約束值為約束對(duì)象的百分比,在Masonry里有對(duì)應(yīng)的multipliedBy函數(shù)
//寬度為superView寬度的20%
make.width.equalTo(superView.mas_width).multipliedBy(0.2);
  • Autolayout下UILabel設(shè)置多行計(jì)算需要設(shè)置preferredMaxLayoutWidth.
    preferredMaxLayoutWidth用來制定最大的寬,一般用在多行的UILabel中;
label.preferredMaxWidth = [UIScreen mainScreen].bounds.size.width - margin - padding;
  • systemLayoutSizeFittingSize方法能夠獲得view的高度;
  • iOS7有兩個(gè)很有用的屬性,topLayoutGuidebottomLayoutGuide,這個(gè)主要是方便獲取UINavigationControllerUITabBarController的頭部視圖區(qū)域和底部視圖區(qū)域。Mosonry直接支持這個(gè)屬性;
make.top.equalTo(self.mas_topLayoutGuide);

二、AutoLayout關(guān)于更新的幾個(gè)方法的區(qū)別


  • setNeedsLayout:告知頁(yè)面需要更新,但不會(huì)立刻開始更新,執(zhí)行后會(huì)立刻調(diào)用layoutSubvies;
  • layoutIfNeeded:告知頁(yè)面布局立刻更新。所以一般都會(huì)和setNeedsLayout一起使用。如果希望立刻生成新的frame需要調(diào)用此方法,利用這一點(diǎn)一般布局動(dòng)畫可以在更新布局后直接使用這個(gè)方法讓動(dòng)畫生效;
  • layoutSubviews:系統(tǒng)重寫布局;
  • setNeedsUpdateConstraints:告知需要更新新約束,但不會(huì)立刻開始;
  • updateConstraintsIfNeeded:告知立刻更新約束;
  • updateConstraints:系統(tǒng)更新約束;

三、Masonry使用注意事項(xiàng)


  • mas_makeConstraints的那個(gè)view需要在addSubview之后才能用這個(gè)方法;
  • mas_equalTo適用數(shù)值元素,equalTo適合多屬性的比如make.left.and.right.equalTo(self.view);
  • 方法and和with只是為了可讀性,返回自身,比如make.left.and.right.equalTo(self.view)make.left.right.equalTo(self.view)是一樣的;
  • 因?yàn)閕OS中原點(diǎn)在左上角所以注意使用offset時(shí)注意right和bottom用負(fù)數(shù)。

四、Masonry使用范例


//相對(duì)于父視圖邊距為10
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(superview.mas_top).with.offset(padding.top); 
    make.left.equalTo(superview.mas_left).with.offset(padding.left);
    make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
    make.right.equalTo(superview.mas_right).with.offset(-padding.right);
}];

//相對(duì)于父視圖邊距為10簡(jiǎn)潔寫法
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(superview).with.insets(padding);
}];

//這兩個(gè)作用完全一樣
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.greaterThanOrEqualTo(self.view);
    make.left.greaterThanOrEqualTo(self.view.mas_left);
}];

//.equalTo .lessThanOrEqualTo .greaterThanOrEqualTo使用NSNumber
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.width.greaterThanOrEqualTo(@200);
    make.width.lessThanOrEqualTo(@400);
    make.left.lessThanOrEqualTo(@10);
}];

//如果不用NSNumber可以用以前的數(shù)據(jù)結(jié)構(gòu),只需用mas_equalTo就行
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(42);
    make.height.mas_equalTo(20);
    make.size.mas_equalTo(CGSizeMake(50, 100));
    make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 10, 0));
    make.left.mas_equalTo(self.view).mas_offset(UIEdgeInsetsMake(10, 0, 10, 0));
}];

//也可以使用數(shù)組
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.height.equalTo(@[self.view.mas_height, superview.mas_height]);
    make.height.equalTo(@[self.view, superview]);
    make.left.equalTo(@[self.view, @100, superview.mas_right]);
}];

// priority的使用
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.greaterThanOrEqualTo(self.view.mas_left).with.priorityLow();
    make.top.equalTo(self.view.mas_top).with.priority(600);
}];

//同時(shí)創(chuàng)建多個(gè)約束
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
    //讓top,left,bottom,right都和self.view一樣
    make.edges.equalTo(self.view);
    //edges
    make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(5, 10, 15, 20));
    //size
    make.size.greaterThanOrEqualTo(self.view);
    make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50));
    //center
    make.center.equalTo(self.view);
    make.center.equalTo(self.view).centerOffset(CGPointMake(-5, 10));
    //chain
    make.left.right.and.bottom.equalTo(self.view);
    make.top.equalTo(self.view);
}];

AutoLayout情況如何計(jì)算UITableView的變高高度


主要是UILabel的高度會(huì)有變化,所以這里主要是說說label變化時(shí)如何處理,設(shè)置UILable的時(shí)候注意要設(shè)置preferredMaxLayoutWidth這個(gè)寬度,還有ContentHuggingPriorityUILayoutPriorityRequried;

CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - 10 * 2;

textLabel = [UILabel new];
textLabel.numberOfLines = 0;
textLabel.preferredMaxLayoutWidth = maxWidth;
[self.contentView addSubview:textLabel];

[textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(statusView.mas_bottom).with.offset(10);
    make.left.equalTo(self.contentView).with.offset(10);
    make.right.equalTo(self.contentView).with.offset(-10);
    make.bottom.equalTo(self.contentView).with.offset(-10);
}];

[_contentLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

如果版本支持最低版本為iOS 8以上的話可以直接利用UITableViewAutomaticDimension在tableview的heightForRowAtIndexPath直接返回即可。

tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 80; //減少第一次計(jì)算量,iOS7后支持

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 只用返回這個(gè)!
    return UITableViewAutomaticDimension;
}

動(dòng)畫


因?yàn)椴季旨s束就是要脫離frame這種表達(dá)方式的,可是動(dòng)畫是需要根據(jù)這個(gè)來執(zhí)行,這里面就會(huì)有些矛盾,不過根據(jù)前面說到的布局約束的原理,在某個(gè)時(shí)刻約束也是會(huì)被還原成frame使視圖顯示,這個(gè)時(shí)刻可以通過layoutIfNeeded這個(gè)方法來進(jìn)行控制。具體代碼如下

//設(shè)置約束
[aniView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.bottom.left.right.equalTo(self.view).offset(10);
}];
//更新約束
[aniView mas_updateConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view).offset(30);
}];
//創(chuàng)建動(dòng)畫
[UIView animateWithDuration:3 animations:^{
    [self.view layoutIfNeeded];
}];

參考鏈接:https://github.com/ming1016/study/wiki/Masonry

?著作權(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ù)。

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

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