Masonry

一、調用Masonry庫的準備

使用pch文件添加全局宏定義(Pre-Compiled Header)
pch文件即 擴展名為.pch的預編譯文件。是將工程中較穩定的不會經常修改的代碼預先編譯好,放在一個公共的文件(.pch)里。

0>將Masonry文件拖入項目中
1>新建pch文件



2>選中項目-->TARGETS-->Build Setting(選中All)-->搜索prefi



3>雙擊并將pch文件拖入框中
4>設置為Yes

5>填入代碼

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"

// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.

#endif /* PrefixHeader_pch */

二 、使用方法

1.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;

下面是關于這些屬性的介紹



2.約束的類型

 1.尺寸:width、height、size
 2.邊界:left、leading、right、trailing、top、bottom
 3.中心點:center、centerX、centerY
 4.邊界:edges
 5.偏移量:offset、inset、insets、sizeOffset、centerOffset
 6.優先級:priority()約束優先級(0~1000)、priorityLow()\*500*\、priorityMedium()\*500*\、priorityHigh()\*750*\
 7.比例:multipliedBy乘因數、 dividedBy除因數

3.兩個區別

1>mas_equalToequalTo:

默認情況下mas_equalTo有自動包裝功能,比如自動將20包裝為@20equalTo沒有自動包裝功能。

如果添加了下面的宏,那么mas_equalToequalTo就沒有區別:
#define MAS_SHORTHAND_GLOBALS

2>mas_heightheight:

默認情況下widthmake對象的一個屬性,用來添加寬度約束用的,表示對寬度進行約束。mas_width是一個屬性值,用來當做equalTo的參數,表示某個控件的寬度屬性。

如果添加了下面的宏,mas_width也可以寫成width:
#define MAS_SHORTHAND

4.基本使用

mas_makeConstraints://添加約束
mas_updateConstraints://更新約束、亦可添加新約束
mas_remakeConstraints://重置之前的約束

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

5.注意點

使用Masonry不需要設置控件的translatesAutoresizingMaskIntoConstraints屬性為NO;
防止block中的循環引用,使用弱引用(這是錯誤觀點),在這里block是局部的引用,block內部引用self不會造成循環引用的
__weak typeof (self) weakSelf = self;(沒必要的寫法)

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

推薦閱讀更多精彩內容

  • Masonry是一個輕量級的布局框架,擁有自己的描述語法,采用更優雅的鏈式語法封裝自動布局,簡潔明了并具有高可讀性...
    3dcc6cf93bb5閱讀 1,828評論 0 1
  • (一)Masonry介紹 Masonry是一個輕量級的布局框架 擁有自己的描述語法 采用更優雅的鏈式語法封裝自動布...
    木易林1閱讀 2,390評論 0 3
  • 安靜的夜,聽著張懸的《寶貝》,莫名的落下淚來,不知為誰。 太多人每天生活在這繁華鬧市,看似歡笑,實則孤獨。什么時候...
    _一方天地_閱讀 687評論 0 1
  • 書中自有黃金屋,千鐘粟,顏如玉。 如何快速閱讀專業書籍,短時間內精通多個領域,把最新的信息裝進腦汁,多一些閑暇的時...
    王二公子閱讀 690評論 5 14
  • 昨晚有幸加入一微課堂體驗繪畫治療,晚上8點準時開始,童老師介紹了什么是繪畫治療,作用療效,和繪畫治療的6個階段(放...
    Nicole觀蓮閱讀 306評論 0 0