Masonry分析

iOS 源代碼分析----Masonry

Masonry是OC自動(dòng)布局的框架,簡(jiǎn)化了AutoLayout的寫(xiě)法。

Autolayout代碼寫(xiě)起來(lái)比較繁瑣
下面是Autolayout設(shè)置上左寬高

//添加 top 約束
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem: label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:64];
[self.view addConstraint:topConstraint];

// 添加 left 約束
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem: label attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
[self.view addConstraint:leftConstraint];

// 添加 width 約束
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem: label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:200]; 
[label addConstraint:widthConstraint];
    
// 添加 height 約束
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem: label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:20];
[label addConstraint:heightConstraint];

Masonry代碼使用比較簡(jiǎn)潔
下面是Masonry設(shè)置上左寬高

[label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@64);
        make.left.equalTo(@0);
        make.width.equalTo(@200);
        make.height.equalTo(@20);
    }];


Masonry常用的方法

1.添加約束常用的方法

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

2.更新和重新設(shè)置約束方法

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

第一個(gè)方法 mas_updateConstraints 更新約束、同事可以添加新的約束
第二個(gè)方法 mas_remakeConstraints 重置約束 把之前的約束全部刪掉

  1. multipliedBy屬性 約束值為約束對(duì)象的百分比
//寬度為self.view寬度的20%
make.width.equalTo(self.view).multipliedBy(0.2);

4.其他的屬性
Masonry有三種關(guān)系:等于(.equalTo)、小于(.lessTahnOrEqualTo)、大于(.greaterThanOrEqualTo)

/**
 1.尺寸:width、height、size
 2.邊界:left、leading、right、trailing、top、bottom
 3.中心點(diǎn):center、centerX、centerY
 4.邊界:edges
 5.偏移量:offset、insets、sizeOffset、centerOffset
 6.priority()約束優(yōu)先級(jí)(0~1000),multipler乘因數(shù), dividedBy除因數(shù)
 */
  • 舉例說(shuō)明
//設(shè)置view的各個(gè)邊距為30
[view makeConstraints:^(MASConstraintMaker *make) {
      make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(30, 30, 30, 30));
 }];

5.使用Masonry時(shí)block內(nèi)部引用self不會(huì)造成循環(huán)引用, translatesAutoresizingMaskIntoConstraints也不用設(shè)置

使用Masonry不需要設(shè)置控件的translatesAutoresizingMaskIntoConstraints屬性為NO;
防止block中的循環(huán)引用,使用弱引用(這是錯(cuò)誤觀點(diǎn)),在這里block是局部的引用,block內(nèi)部引用self不會(huì)造成循環(huán)引用的
__weak typeof (self) weakSelf = self;(沒(méi)必要的寫(xiě)法)

?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容