iOS 開發中自動布局之 masonry 第三方框架的使用

一、先做一個比較

  • 使用系統自帶的方法
    UIView *superview = self.view;

    //創建一個 view1
    UIView *view1 = [[UIView alloc] init];
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    view1.backgroundColor = [UIColor greenColor];
    [superview addSubview:view1];
    
    UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

    [superview addConstraints:@[
            //view1 constraints
            [NSLayoutConstraint constraintWithItem:view1
                                         attribute:NSLayoutAttributeTop
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:superview
                                         attribute:NSLayoutAttributeTop
                                        multiplier:1.0
                                          constant:padding.top],
            
            [NSLayoutConstraint constraintWithItem:view1
                                         attribute:NSLayoutAttributeLeft
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:superview
                                         attribute:NSLayoutAttributeLeft
                                        multiplier:1.0
                                          constant:padding.left],
            
            [NSLayoutConstraint constraintWithItem:view1
                                         attribute:NSLayoutAttributeBottom
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:superview
                                         attribute:NSLayoutAttributeBottom
                                        multiplier:1.0
                                          constant:-padding.bottom],
            
            [NSLayoutConstraint constraintWithItem:view1
                                         attribute:NSLayoutAttributeRight
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:superview
                                         attribute:NSLayoutAttributeRight
                                        multiplier:1
                                          constant:-padding.right]
            ]];
  • 使用 masonry 實現同樣效果,代碼進行比較:
  • 第一種方法:
    UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
    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);
    }];
  • 第二種方法:
    UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(superview).with.insets(padding);
}];

二、masonry 的簡單使用

2.1.創建一個 size 為 (100,100) 大小,位置位于右下角的 view:
  • 首先創建一個 view:yellow
    UIView *yellow = [[UIView alloc]init];
    yellow.backgroundColor = [UIColor yellowColor];
    yellow.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:yellow];

注:創建完 view 控件之后就要加入父控件中,因為添加約束時候會參照父控件進行約束,如果不這樣做運行時就會在約束處報錯。

  • 添加約束方法一:
    [yellow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.bottom.mas_equalTo(self.view).offset(-20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
    }];
  • 添加約束方法二:
    [yellow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.bottom.offset(-20);
        make.right.mas_equalTo(self.view).offset(-20);
    }];
  • 添加約束方法三:
[yellow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(100));
        make.height.mas_equalTo(100);
        make.right.offset(-20);
        make.bottom.mas_equalTo(self.view).offset(-20);
    }];

總結:①方法一和方法二所添加約束代碼有所不同,通過仔細觀察可以發現以下三句代碼所起作用一樣,系統會根據 make. 后面的關鍵詞進行自動匹配后面的內容:

make.bottom.mas_equalTo(self.view.mas_bottom).offset(-20);
make.bottom.mas_equalTo(self.view).offset(-20);
make.bottom.offset(-20);
  • 如果后面有 mas_equalTo 就進行匹配;
  • 如果句中沒有 mas_equalTo 就會自動添加和 make. 后面的關鍵詞對應的約束。

②方法三種 make.width.equalTo(@(100))make.width.equalTo 中傳入的參數要是 id 類型的,所以必須要對基本數據類型進行封裝;而 make.height.width.mas_equalTo(100) 中的 mas_equalTo 會對傳入的參數進行自動封裝,所以對傳入的參數可以不封裝。

2.2.創建一個大小為父控件大小 0.3 倍的控件,并居中顯示:
  • 方法一:
  [yellow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.view).multipliedBy(0.3);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.centerY.mas_equalTo(self.view.mas_centerY);
  }];

以上約束 X 和 Y 的語句中,都可以再進行更改,例如把 mas_equalTo(self.view.mas_centerY) 改成 mas_equalTo(self.view),效果一樣。

  • 方法二:
  [yellow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.view).multipliedBy(0.3);
        make.center.mas_equalTo(self.view.center);
  }];

同樣:以上代碼中的 mas_equalTo(self.view.center) 可以把參數中的 **.center **去掉。

三、總結:

  • 一般的項目都很少使用系統提供的自動布局方法,轉而使用 masonry 第三方框架;
  • mas_equalTo 和 equalTo 比較:
  • mas_equalTo:這個方法會對參數進行包裝;
  • equalTo:這個方法不會對參數進行包裝;
  • mas_equalTo 的功能強于 equalTo 。
  • 優化 mas_
  • 加入下面這句代碼** #define MAS_SHORTHAND** ,在所有地方都可以把 mas_ 去掉,但是此句必須要加在 **#import "本類類名"; ** 之前(即導入本類頭文件之前);
  • 但是有些 mas_ 不能去掉,比如:make.width.mas_equalTo(100);必須要進行包裝,此時在 #import "本類類名" 之前加入以下一句即可:#define MAS_SHORTHAND_GLOBALS
  • 建議:
    • 在不確定的時候,所有地方都使用帶有 mas_ 的關鍵字;
    • 在文件中引入 ** #define MAS_SHORTHAND** 和 #define MAS_SHORTHAND_GLOBALS 之后,就不需要在考慮任何 mas_
  • 約束的類型:
  • 尺寸:width/height/size
  • 邊界:left/leading/right/trailing/top/bottom
  • 中心的:center/centerX/centerY
  • 邊界:edges

總之,masonry 的語法很靈活,勤加練習,找到規律,使用起來就會覺得很輕松了 O(∩_∩)O

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一、前言 關于蘋果的布局一直是我比較糾結的問題,是寫代碼來控制布局,還是使用storyboard來控制布局呢?以前...
    iplaycodex閱讀 2,476評論 0 1
  • (一)Masonry介紹 Masonry是一個輕量級的布局框架 擁有自己的描述語法 采用更優雅的鏈式語法封裝自動布...
    木易林1閱讀 2,390評論 0 3
  • Masonry是一個輕量級的布局框架,擁有自己的描述語法,采用更優雅的鏈式語法封裝自動布局,簡潔明了并具有高可讀性...
    3dcc6cf93bb5閱讀 1,828評論 0 1
  • iOS_autoLayout_Masonry 概述 Masonry是一個輕量級的布局框架與更好的包裝AutoLay...
    指尖的跳動閱讀 1,195評論 1 4
  • 一位朋友說你要記得亂語的限制,那是經驗之談也是初心,帶領者要敏感你所帶群組的能量 經過Amma的撬...
    貓秘閱讀 629評論 0 0