masonry學(xué)習(xí)

masonry 屬性:

@property (nonatomic, strong, readonly) MASConstraint *left;        //左側(cè)

@property (nonatomic, strong, readonly) MASConstraint *top;        //上側(cè)

@property (nonatomic, strong, readonly) MASConstraint *right;      //右側(cè)

@property (nonatomic, strong, readonly) MASConstraint *bottom;  //下側(cè)

@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; //文本基線

使用:

1,先創(chuàng)建一個(gè)View

UIView *sv = [UIView new];

sv.backgroundColor = [UIColor blackColor];

2,添加到父視圖上

//autolayout之前,先將View添加到supview上

[self.view addSubview:sv];

3,設(shè)置約束

[sv mas_makeConstraints:^(MASConstraintMaker *make) {

//設(shè)置sv的中心

make.center.equalTo(ws.view);

//設(shè)置sv的寬高

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

}];

4,創(chuàng)建了一個(gè)新的View,添加到上一個(gè)view上,并設(shè)置約束

UIView *sv1 = [UIView new];

sv1.backgroundColor = [UIColor redColor];

[sv addSubview:sv1];

[sv1 mas_makeConstraints:^(MASConstraintMaker *make) {

//設(shè)置各個(gè)方向的縮進(jìn)(三種方法)

(1)// make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 12, 10));

(2)/*make.top.equalTo(sv).with.offset(10);(上)

make.left.equalTo(sv).with.offset(10);(左)

make.bottom.equalTo(sv).with.offset(-10);(下)

make.right.equalTo(sv).with.offset(-10);(右)*/

(3)make.top.left.bottom.and.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));

}];

注意:make.right.equalTo(sv).with.offset(10);

make.right.equalTo(sv.mas_left).with.offset(10);

這兩行表示含義不同,前者是相對(duì)sv的右邊,后者是相對(duì)sv的左邊.也就是說(shuō)當(dāng)equalTo后面沒(méi)寫相對(duì)哪一邊時(shí)默認(rèn)和make.后面的方向一致.

/*

mas_makeConstraints只負(fù)責(zé)新增的約束,autoLayout不能同時(shí)存在兩條針對(duì)同一對(duì)象的約束,負(fù)責(zé)會(huì)報(bào)錯(cuò)

mas_updateConstraints針對(duì)上面的情況,會(huì)更新在block中出現(xiàn)的約束,不會(huì)導(dǎo)致出現(xiàn)兩個(gè)同一約束的情況,必須針對(duì)同一個(gè)參照對(duì)象,使用時(shí)必須加上[super updateViewConstraints];這句代碼

mas_remakeConstraints則會(huì)清除之前所有的約束,僅僅保留最新的約束,block里面就是你所要添加的新約束.

*/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容