Masonry

1.mas_equalTo和equalTo

默認情況下,mas_equalTo有自動包裝功能,比如自動將20包裝為@20
equalTo沒有自動包裝功能
如果添加了下面的宏,那么mas_equalTo和equalTo就沒有區別

#define MAS_SHORTHAND_GLOBALS
// 注意:這個宏一定要添加到#import "Masonry.h"前面
// 藍色控件
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
    // 尺寸限制:100x100
    // 位置:粘著父控件右下角,間距是20
    
    // 這個方法只會添加新的約束
       [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
            // 寬度約束
            make.width.equalTo(@100);
            // 高度約束
            make.height.equalTo(@100);
            // 右邊
            make.right.equalTo(self.view.mas_right).offset(-20);
            // 頂部
            make.top.equalTo(self.view.mas_top).offset(20);
        }];
    
        [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
            // 寬度約束
            make.width.mas_equalTo(100);
            // 高度約束
            make.height.mas_equalTo(100);
            // 右邊
            make.right.equalTo(self.view).offset(-20);
            // 頂部
            make.top.equalTo(self.view).offset(20);
        }];
    
        [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
            // 寬度高度約束
            make.width.height.mas_equalTo(100);
            // 右邊
            make.right.equalTo(self.view).offset(-20);
            // 頂部
            make.top.equalTo(self.view).offset(20);
        }];
    
        [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
            // 寬度高度約束
    //        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(100, 100)]);
    //        make.size.mas_equalTo(CGSizeMake(100, 100));
            make.size.mas_equalTo(100);
            // 右邊
            make.right.equalTo(self.view).offset(-20);
            // 頂部
            make.top.equalTo(self.view).offset(20);
        }];
    
        [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
            // 寬度高度約束
            make.height.mas_equalTo(self.view).multipliedBy(0.5).offset(-50);
            // 右邊
            make.right.mas_equalTo(self.view).offset(-20);
            //        make.right.offset(-20);
            // 頂部
            make.top.mas_equalTo(self.view).offset(20);
           //        make.top.offset(20);
        }];

2.width和mas_width

默認情況下,width是make對象的一個屬性,用來添加寬度約束用的,表示對寬度進行約束
mas_width是一個屬性值,用來當做equalTo的參數,表示某個控件的寬度屬性
如果添加了下面的宏,mas_width也可以寫成width

#define MAS_SHORTHAND
mas_height、mas_centerX以此類推
// 藍色控件
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
    // 紅色控件
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    
    // 添加約束
    CGFloat margin = 20;
    CGFloat height = 50;
    [blueView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(margin);
        make.right.equalTo(redView.left).offset(-margin);
        make.bottom.equalTo(self.view.bottom).offset(-margin);
        make.height.equalTo(height);
        make.top.equalTo(redView.top);
        make.bottom.equalTo(redView.bottom);
        make.width.equalTo(redView.width);
    }];
    
    [redView makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.right).offset(-margin);
    }];

3.以下方法都僅僅是為了提高可讀性,可有可無

- (MASConstraint *)with {
    return self;
}

- (MASConstraint *)and {
    return self;
}

4.約束的類型:

  1. 尺寸:width\height\size
  2. 邊界:left\leading\right\trailing\top\bottom
  3. 中心點:center\centerX\centerY
  4. 邊界:edges
- (void)test4
{
    // 藍色控件
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
    // 添加約束
    [blueView makeConstraints:^(MASConstraintMaker *make) {
        //        make.width.equalTo(self.view.width).multipliedBy(0.5);
        //        make.height.equalTo(self.view.height).multipliedBy(0.5).offset(-100);
        make.width.equalTo(100);
        make.height.equalTo(100);
        make.centerX.equalTo(self.view.centerX);
        make.centerY.equalTo(self.view.centerY);
    }];
}

- (void)test3
{
    // 藍色控件
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
    // 距離父控件四周都是50間距
    //    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.mas_equalTo(self.view.mas_left).offset(50);
    //        make.right.mas_equalTo(self.view.mas_right).offset(-50);
    //        make.top.mas_equalTo(self.view.mas_top).offset(50);
    //        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-50);
    //    }];
    //    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.mas_equalTo(self.view).offset(50);
    //        make.right.mas_equalTo(self.view).offset(-50);
    //        make.top.mas_equalTo(self.view).offset(50);
    //        make.bottom.mas_equalTo(self.view).offset(-50);
    //    }];
    //    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.offset(50);
    //        make.right.offset(-50);
    //        make.top.offset(50);
    //        make.bottom.offset(-50);
    //    }];
    //    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.top.offset(50);
    //        make.right.bottom.offset(-50);
    //    }];
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.edges.mas_equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));
        make.center.mas_equalTo(self.view).insets(UIEdgeInsetsZero);
    }];
}

- (void)test2
{
    // 藍色控件
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
    // 居中(水平+垂直)
    // 尺寸是父控件的一半
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self.view).multipliedBy(0.5);
        make.center.mas_equalTo(self.view);
        //        make.centerX.mas_equalTo(self.view);
        //        make.centerY.mas_equalTo(self.view);
    }];
}

5.remakeConstraints和updateConstraints

// 這個方法會將以前的所有約束刪掉,添加新的約束
 [blueView mas_remakeConstraints:^(MASConstraintMaker *make) {
 
 }];
// 這個方法將會覆蓋以前的某些特定的約束
 [blueView mas_updateConstraints:^(MASConstraintMaker *make) {
 
 }];

6.Masonry自動計算cell行高

(1)導入UITableViewCell+HYBMasonryAutoCellHeight和UITableView+HYBCacheHeight.h這個分類
(2)設置關鍵依賴:
要想自動計算出 cell 的行高,我們還需要指定以哪個視圖作為 cell 的最后一個視圖,比如我們最后要添加一條線,我們可以以這條線作為 hyb_lastViewInCell ,如果這條線還需要距離底部一定距離,那么可以設置 hyb_bottomOffsetToCell 。
一般在cell 的configCellWithModel: indexPath:方法中設置這兩個屬性。
這兩個屬性在分類中,直接可以使用。

- (void)configCellWithModel:(InteractCommentModel *)infoModel indexPath:(NSIndexPath *)indexPath {
    self.infoModel = infoModel;
    
    self.indexPath = indexPath;

    [_headImg sd_setImageWithURL:[NSURL URLWithString:infoModel.commUserHeadPic] placeholderImage:[UIImage imageNamed:@"SZ_wodetouxiang"]];
    if (![infoModel.commUserNick isEqualToString:@""]) {
        _name.text = infoModel.commUserNick;
    }else{
        _name.text = @"匿名用戶";
    }

    _comment.text = infoModel.commContent;

    _time.text = infoModel.commTime;
    
    if ([infoModel.commUserLevel isEqualToString:@"2"]) {
        //_commentButton.hidden = YES;
        _mark.hidden = NO;

    }

        
        [_line mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
            
        }];

        [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
            
        }];

        [_moreBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];

        
        _tableView.hidden = YES;
        _moreBtn.hidden = YES;
        _line.hidden = YES;

        self.hyb_lastViewInCell = self.comment;
        self.hyb_bottomOffsetToCell = 8;


    }

(3)要計算行高,只需要在 UITableView 的計算行高的代理方法中調用此API即可:

/**
* 通過此方法來計算行高,需要在config中調用配置數據的API
*
* @param indexPath 必傳,對應的indexPath
* @param confi     必須要實現,且需要調用配置數據的API
*
* @return 計算的行高
*/
+ (CGFloat)hyb_heightForIndexPath:(NSIndexPath *)indexPathconfig:(HYBCellBlock)config;

在調用時, config 傳回來了 cell 對象,需要在調用處調用方法來配置好數據,才能正確地計算出 cell 的行高。通常是這樣調用的:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    InteractCommentModel * infoModel = self.listArry[indexPath.row];
    
    CGFloat h = [BaseInteractCommentCell hyb_heightForTableView:tableView config:^(UITableViewCell *sourceCell) {
        BaseInteractCommentCell *cell = (BaseInteractCommentCell *)sourceCell;
        [cell configCellWithModel:infoModel indexPath:indexPath];
    } cache:^NSDictionary *{
        NSDictionary *cache = @{kHYBCacheUniqueKey : infoModel.commId,
                                kHYBCacheStateKey  : @"",
                                kHYBRecalculateForStateKey : @(infoModel.ShouldUpdateCache)};
        infoModel.ShouldUpdateCache = YES;
        return cache;
    }];
    
    return h;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,563評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,694評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,672評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,965評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,690評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,019評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,013評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,188評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,718評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,438評論 3 360
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,667評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,149評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,845評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,252評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,590評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,384評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,635評論 2 380

推薦閱讀更多精彩內容