Masonry使用

Masonry是一款輕量級(jí)的布局框架,采用方便的鏈?zhǔn)秸Z(yǔ)法,可以更方便的幫助我們使用純代碼布局

下載地址:https://github.com/SnapKit/Masonry

導(dǎo)入方式:#import "Masonry.h"

約束的三種方法


//添加新約束

- (NSArray *)mas_makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

//更新約束,會(huì)覆蓋之前的約束

- (NSArray *)mas_updateConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

//完全移除舊約束,添加新約束

- (NSArray *)mas_remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

約束的基本屬性


@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;

約束的特殊屬性


//(top, left, bottom, right)

@property (nonatomic, strong, readonly) MASConstraint *edges;

//(width, height)

@property (nonatomic, strong, readonly) MASConstraint *size;

//(centerX, centerY)

@property (nonatomic, strong, readonly) MASConstraint *center;

注意點(diǎn):

1.使用Masonry不需要設(shè)置控件的translatesAutoresizingMaskIntoConstraints屬性為NOmas_makeConstraintsmas_updateConstraints以及mas_remakeConstraints內(nèi)部已經(jīng)幫我們?cè)O(shè)置了;


- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {

 self.translatesAutoresizingMaskIntoConstraints = NO;

 MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];

 block(constraintMaker);

 return [constraintMaker install];

}

2.mas_makeConstraintsmas_updateConstraints以及mas_remakeConstraintsblock是局部引用,block 里用到了 self,那 block 會(huì)保持一個(gè) self 的引用,但是 self 并沒(méi)有直接或者間接持有 block,所以不會(huì)造成循環(huán)引用。因此__weak typeof (self) weakSelf = self是沒(méi)必要的

3.添加約束一定要放addSubview之后,否則程序會(huì)崩潰

基本使用

Masonry.png
 UIView *blueView = [[UIView alloc]init];

 blueView.backgroundColor = [UIColor blueColor];

 [self.view addSubview:blueView];

 [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

 //設(shè)置blueView的中心點(diǎn)的x等于self.view的中心點(diǎn)的x

 make.centerX.equalTo(self.view);

 //設(shè)置blueView的頂部等于self.view的頂部,同時(shí)偏移30

 make.top.equalTo(self.view).offset(30);

 //設(shè)置寬高都為90

 make.size.mas_equalTo(CGSizeMake(90, 90));

 }];

 UIView *blackView = [[UIView alloc]init];

 blackView.backgroundColor = [UIColor blackColor];

 [self.view addSubview:blackView];

 [blackView mas_makeConstraints:^(MASConstraintMaker *make) {

 make.top.equalTo(blueView.mas_bottom).offset(20);

 make.right.equalTo(blueView.mas_left).offset(20);

 make.size.mas_equalTo(CGSizeMake(100, 100));

 }];

 UIView *greenView = [[UIView alloc]init];

 greenView.backgroundColor = [UIColor greenColor];

 [self.view addSubview:greenView];

 [greenView mas_makeConstraints:^(MASConstraintMaker *make) {

 make.size.mas_equalTo(CGSizeMake(200, 50));

 make.center.equalTo(self.view);

 }];

equalTo和mas_equalTo的區(qū)別

先觀察源碼:


#define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__)))

mas_equalTo會(huì)對(duì)參數(shù)進(jìn)行一個(gè)MASBoxValue()操作,可以支持的類型是:NSNumber、 CGPoint、CGSize、UIEdgeInsets


/**

 *  Sets the constraint relation to NSLayoutRelationEqual

 * returns a block which accepts one of the following:

 * MASViewAttribute, UIView, NSValue, NSArray

 * see readme for more details.

 */

- (MASConstraint * (^)(id attr))equalTo;

equalTo并沒(méi)有MASBoxValue()操作,源碼的注釋上說(shuō)了equalTo支持的類型是:MASViewAttribute, UIView, NSValue, NSArray

為了方便編碼的2個(gè)宏

1.#define MAS_SHORTHAND:可以省略'mas_'前綴

2.#define MAS_SHORTHAND_GLOBALS:自動(dòng)將結(jié)構(gòu)體包裝成 NSValue,而無(wú)需再使用 mas_equalTo,其源碼如下:


#ifdef MAS_SHORTHAND_GLOBALS

#define equalTo(...) mas_equalTo(__VA_ARGS__)

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