使用Masonry布局 前面布局的控件被后面布局的label拉伸问题

使用Masonry布局 前面布局的控件被后面布局的label拉伸问题_第1张图片

如图运行后后面的label约束使得前面删除按钮被拉长,源代码如下:

_delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_delBtn addTarget:self action:@selector(del) forControlEvents:UIControlEventTouchUpInside];
        [_delBtn setImage:[UIImage imageNamed:@"删除1"] forState:UIControlStateNormal];
        [self addSubview:_delBtn];
        [_delBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.centerY.mas_equalTo(self);
            make.height.with.mas_equalTo(30);
        }];
        
        _selectImageView = [[UIImageView alloc] init];
        _selectImageView.image = [UIImage imageNamed:@"对勾"];
        _selectImageView.hidden = YES;
        [self addSubview:_selectImageView];
        [_selectImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-10);
            make.width.height.mas_equalTo(20);
            make.centerY.mas_equalTo(self);
        }];
        
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = THREECOLOR;
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_delBtn.mas_right).offset(10);
            make.right.equalTo(_selectImageView.mas_left).offset(-5);
            make.height.mas_offset(16);
            make.centerY.mas_equalTo(self);
        }];

更改后如下图:

使用Masonry布局 前面布局的控件被后面布局的label拉伸问题_第2张图片

更改后代码:

_delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_delBtn addTarget:self action:@selector(del) forControlEvents:UIControlEventTouchUpInside];
        [_delBtn setImage:[UIImage imageNamed:@"删除1"] forState:UIControlStateNormal];
        [self addSubview:_delBtn];
        [_delBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.centerY.mas_equalTo(self);
            make.height.with.mas_equalTo(30);
        }];
        
        _selectImageView = [[UIImageView alloc] init];
        _selectImageView.image = [UIImage imageNamed:@"对勾"];
        _selectImageView.hidden = YES;
        [self addSubview:_selectImageView];
        [_selectImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-10);
            make.width.height.mas_equalTo(20);
            make.centerY.mas_equalTo(self);
        }];
        
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = THREECOLOR;
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_delBtn.mas_right).offset(10);
            make.right.mas_lessThanOrEqualTo(_selectImageView.mas_left).offset(-5);
            make.height.mas_offset(16);
            make.centerY.mas_equalTo(self);
        }];

主要是label向右约束时用mas_lessThanOrEqualTo

你可能感兴趣的:(备忘)