iOS开发之约束特殊用法

我们首先来看一张图


需求:

  1. 昵称和时间是两个label,添加约束的时候不能限制宽度
  2. 时间label必须全部展示,不能省略,两个label长度大于剩余空间的时候要省略昵称(昵称不限制长度)

解决:

  • 第一步:添加相关约束
   //名字
    [self.name_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.name_imageV.mas_centerY);
        make.left.equalTo(weakSelf.name_imageV.mas_right).offset(pading);
    }];
    //时间图标
    [self.time_imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.name_imageV.mas_centerY);
        make.left.equalTo(weakSelf.name_label.mas_right).offset(spaceing);
        make.width.height.offset(13);
    }];
    //时间
    [self.time_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.name_imageV.mas_centerY);
        make.left.equalTo(weakSelf.time_imageV.mas_right).offset(pading);
        make.right.lessThanOrEqualTo(weakSelf.superView.mas_right).offset(-50);
    }];
  • 第二步:设置优先级<重点>:懒加载内部要设置这个优先级
- (UILabel *)time_label
{
    if (!_time_label) {
        _time_label = [[UILabel alloc] init];
        _time_label.textColor = ColorFromRGB(103, 103, 103, 1.0);
        [_time_label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }
    return _time_label;
}

这篇文章感觉写的逻辑思路不是很清晰,时间仓促,有什么疑问小伙伴们可以评论区留言给我

你可能感兴趣的:(iOS开发之约束特殊用法)