iOS Masonry使用指北

<article class="_2rhmJa">

第三方庫地址:https://github.com/SnapKit/Masonry
pod 'Masonry'

(UIKit - 中有系統(tǒng)的自動布局)

一般的布局

    self.letfView = [UIView new];
    self.letfView.backgroundColor = [UIColor redColor];
    self.rightView = [UIView new];
    self.rightView.backgroundColor = [UIColor greenColor];

    [self.view addSubview:self.letfView];
    [self.view addSubview:self.rightView];

    [self.letfView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).mas_offset(10);// leftView 左邊 = self.view 左邊 +10
        make.top.mas_equalTo(self.view).mas_offset(20);// leftView 上邊 = self.view 上邊 +20
        make.height.mas_equalTo(100);// leftView 高 = 100
    }];

    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.letfView);// rightView 上邊 = leftView 上邊(即上邊對齊)
        make.right.mas_equalTo(self.view).mas_offset(-10);// rightView 右邊 = self.view 右邊 - 10
        make.left.mas_equalTo(self.letfView.mas_right).mas_offset(20);// rightView 左邊 = leftView 右邊 + 20
        make.width.mas_equalTo(self.letfView);// rightView 寬 = leftView 寬
        make.height.mas_equalTo(self.letfView);// rightView 高 = leftView 高
    }];

// 實現(xiàn)了最基礎(chǔ)的 左右2個View 等高等寬等簡單約束,在橫豎屏切換通用。

到這里,基本上已經(jīng)可以開始玩masonry了,看完更多屬性,基本小學(xué)畢業(yè)了。

更多 make.XXX ,先手比較屬性。

left; 左
top; 上
right; 右
bottom; 下
leading; 左
trailing; 右
width; 寬
height; 高
centerX; x軸中心
centerY; y軸中心
baseline; 基線,沒怎么用過

leftMargin; 左邊默認(rèn)邊距好像是20,下面的類似
rightMargin;
topMargin;
bottomMargin;
leadingMargin;
trailingMargin;
centerXWithinMargins;
centerYWithinMargins;

----------- 分割線 ----------

edges;4周
size;大小
center;中心
對應(yīng)語法略有不同,偏移方法也相對復(fù)雜一些。不偏移的話使用可以與上面一致。

更多mas_equalTo(XXX) ,后手比較屬性

上半部分 基本雷同,如果不添加,就是默認(rèn)與前面make.XXX 對應(yīng)的mas_
mas_left;
mas_top;
mas_right;
mas_bottom;
mas_leading;
mas_trailing;
mas_width;
mas_height;
mas_centerX;
mas_centerY;
mas_baseline;

mas_leftMargin;
mas_rightMargin;
mas_topMargin;
mas_bottomMargin;
mas_leadingMargin;
mas_trailingMargin;
mas_centerXWithinMargins;
mas_centerYWithinMargins;

----------- 分割線 ----------

自動根據(jù)bar 高度設(shè)置的引導(dǎo)屬性值,舉個例子:
存在navigationBar 時,mas_topLayoutGuideBottom 相當(dāng)于 增加了44。
不存在navigationBar 時,mas_topLayoutGuideBottom 相對于 0 。

mas_topLayoutGuide;// navgationBar 相關(guān),
mas_topLayoutGuideTop;
mas_topLayoutGuideBottom;

mas_bottomLayoutGuide;// tabbar toolbar 相關(guān)
mas_bottomLayoutGuideTop;
mas_bottomLayoutGuideBottom;

多條件布局

  • 大于、小于、等于
mas_equalTo; =
mas_greaterThanOrEqualTo; >=
mas_lessThanOrEqualTo; <=
// 大小于,使用場景感覺比較少。

  • 約束優(yōu)先級
簡單舉例:原本2個View是上下排布,當(dāng)topView 移除時,bottomView,就使用次級約束而上移。

    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.right.and.left.mas_equalTo(self.view);
        make.height.mas_equalTo(100);
    }];

    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topView.mas_bottom).priority(999);// 可以自己寫優(yōu)先值,越大越優(yōu)先(1000 是xib 默認(rèn)值,也是系統(tǒng)默認(rèn)最大值)
        make.top.mas_equalTo(self.view).priorityLow();// 也可以用系統(tǒng)默認(rèn)的優(yōu)先值,low 代表250

        make.left.and.right.mas_equalTo(self.view);
        make.height.mas_equalTo(100);
    }];

  • 同時多個屬性一致簡寫
// 使用 and 鏈接屬性
make.left.and.top.and.width.and.height.mas_equalTo(self.letfView);

  • 同時與多個View存在關(guān)系,混合寫
// 數(shù)組形式    make.top.mas_equalTo(@[self.letfView.mas_top,self.rightView.mas_top]);

  • mas_equalTo(XXX) 的 mas_width 與 width 比較
// mas_ 前綴的 是宏定義,封裝好了直接可以使用 NSInteger,
// 而沒有前綴的 需要使用 NSNumber
        make.width.mas_equalTo(100);
        make.width.equalTo(@100);

  • mas_offset 與 with.offset 相比較

    UIEdgeInsets edg = UIEdgeInsetsMake(10, 20, 30, 40);

    [self.letfView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view).mas_offset(edg);
        make.edges.mas_equalTo(self.view).with.insets(edg);

    }];

// 使用 with. 需要區(qū)分 offset、insets、sizeOffset、centerOffset,分別使用偏移。
// 使用mas_offset 自動區(qū)分了上面的幾種情況,自動匹配了

關(guān)于 偏移 可以繼續(xù)研究CoreGraphice 框架中的 CGGeometry,下次在寫。

更新 重置約束 - 可以實現(xiàn)動畫效果

設(shè)置約束三大方法

mas_makeConstraints 添加某個
mas_updateConstraints 更新某個
mas_remakeConstraints 重置全部

  • 簡單更新約束
    // 原本 約束
    [self.letfView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.right.and.left.mas_equalTo(self.view);
        make.height.mas_equalTo(100);
    }];

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 更新約束
    [self.letfView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).mas_offset(300);
    }];

  • 結(jié)合平移動畫
    // 修改 上面的 更新約束的方法,
    [self.letfView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).mas_offset(300);
    }];

    [self.letfView setNeedsLayout]; // 標(biāo)記需要更新約束
    [UIView animateWithDuration:1. animations:^{
        [self.letfView layoutIfNeeded]; // 動畫過程中更新約束,達(dá)到偏移效果
    }];

布局涉及的其他屬性 - 內(nèi)容抗壓縮,內(nèi)容緊湊

(我的理解,叫法可能不一樣)

舉個例子:同水平方向有2 個 Label,內(nèi)容都不確定有多少,即寬度不定。

  • 對2個Label 進(jìn)行不定寬的布局,如果僅僅這樣布局,對內(nèi)容的控制,可能不是我們想要的。但是好像沒有報錯。可能默認(rèn)處理了。(我這里嘗試與第一個label 抗壓縮,第二個label 內(nèi)容優(yōu)先緊湊,效果一致。)

    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.and.left.mas_equalTo(self.view);
    }];

    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.and.right.mas_equalTo(self.view);
        make.left.mas_equalTo(self.leftLabel.mas_right);
    }];

  • 對Label 添加屬性
(個人覺得第一步是這個:先顯示內(nèi)容)
    // 內(nèi)容緊湊 - 優(yōu)先完全顯示內(nèi)容,且不多占像素。
    [self.leftLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [self.rightLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];

// (個人覺得第二步是這個:調(diào)整內(nèi)容是否壓縮)
    // 抵抗 壓縮,抗壓縮低的,在上一步的基礎(chǔ)上,進(jìn)行壓縮調(diào)整。
    [self.leftLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [self.rightLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];

作者鏈接:http://m.itdecent.cn/p/47f01594d031

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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