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

你可能感兴趣的:(Masonry)