IOS控件布局之Masonry布局框架


前言:

  回想起2013年做iOS開發的時候,那時候並沒有采用手寫布局代碼的方式,而是采用xib文件來編寫,如果使用純代碼方式是基於window的size(320,480)計算出一個相對位置進行布局,那個時候windows的size是固定不變的,隨着iphone5的發布,windows的size(320,568)也發生了變化,而采用autoresizingMask的方式進行適配,到后來iphone 6之后windows size的寬度也隨之變化,開始拋棄autoresizingMask改用autolayout了,使用autolayout進行適配我也是最近重新做iOS開發才接觸的,公司使用Masonry框架進行布局適配。所以學習使用這個布局框架對我來說至關重要,它大大提高了開發效率而且最近使用起來很多語法和Android有很大的相似之處。

什么是Masonry?

  Masonry是一個輕量級的布局框架,擁有自己的描述語法,采用更優雅的鏈式語法封裝自動布局、簡潔明了、 並具有高可讀性、 而且同時支持 iOS 和 Max OS X。

如何使用?

1.)引入頭文件

我這里是在全局引用pch文件中引用的

#import "Masonry.h"

2.)基本語法

Masonry提供的屬性

  • @property (nonatomic, strong, readonly) MASConstraint *left;//左側
  • @property (nonatomic, strong, readonly) MASConstraint *top;//上側
  • @property (nonatomic, strong, readonly) MASConstraint *right;//右側
  • @property (nonatomic, strong, readonly) MASConstraint *bottom;//下側
  • @property (nonatomic, strong, readonly) MASConstraint *leading;//首部
  • @property (nonatomic, strong, readonly) MASConstraint *trailing;//尾部
  • @property (nonatomic, strong, readonly) MASConstraint *width;//寬
  • @property (nonatomic, strong, readonly) MASConstraint *height;//高
  • @property (nonatomic, strong, readonly) MASConstraint *centerX;//橫向居中
  • @property (nonatomic, strong, readonly) MASConstraint *centerY;//縱向居中
  • @property (nonatomic, strong, readonly) MASConstraint *baseline;//文本基線

Masonry提供了三個函數方法

  • - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; //新增約束
  • - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;//更新約束
  • - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;//清楚之前的所有約束,只會保留最新的約束

我們根據不同的使用場景來選擇使用不同的函數方法。

3.)具體舉例

  比如一個往父控件中添加一個上下左右與父控件間距為50的子視圖

添加約束

    UIView *tempView=[[UIView alloc]init];
    tempView.backgroundColor=[UIColor greenColor];
    [self.view addSubview:tempView];
    
    [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-50);
        make.top.mas_equalTo(50);
        make.bottom.mas_equalTo(-50);
    }];

等價於

    UIView *tempView=[[UIView alloc]init];
    tempView.backgroundColor=[UIColor greenColor];
    [self.view addSubview:tempView];
    
    [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(50);
        make.right.equalTo(self.view.mas_right).offset(-50);
        make.top.equalTo(self.view.mas_top).offset(50);
        make.bottom.equalTo(self.view.mas_bottom).offset(-50);
    }];

也可以簡化為下面這種

    UIView *tempView=[[UIView alloc]init];
    tempView.backgroundColor=[UIColor greenColor];
    [self.view addSubview:tempView];
    
    [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(50, 50, 50, 50));
    }];

又等價於

    UIView *tempView=[[UIView alloc]init];
    tempView.backgroundColor=[UIColor greenColor];
    [self.view addSubview:tempView];
    
    [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));
    }];

更新約束

    [tempView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-50);
        make.top.mas_equalTo(100);
        make.bottom.mas_equalTo(-100);
    }];

清除之前的約束保留最新的

    [tempView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(100);
        make.right.mas_equalTo(-100);
        make.top.mas_equalTo(100);
        make.bottom.mas_equalTo(-100);
    }];

特別注意:

   聲明約束必須在視圖添加到父試圖上面之后調用。

4.)mas_equalTo與equalTo

 上面的舉例中分別使用了mas_equalTo和equalTo達到了同樣的效果,我在剛開始使用Masonry的時候很容易混淆他們兩個,今天特意分析一下他們的區別。mas_equalTo是一個MACRO,比較的是值,equalTo比較的是id類型。

總結:

  簡單記錄一下Masonry布局框架的使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM