此代码通过设置自动布局约束条件来确保 timeLabel 始终完全显示,并且与 titleLabel 保持 12 点的间距。通过调整内容抗拉伸优先级和内容压缩抗拉伸优先级,确保 timeLabel 优先显示, titleLabel 根据剩余空间调整其宽度。
//标题
[self.contentView addSubview:self.titleLabel];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.iconImageView.mas_right).mas_offset(SLMargin);
make.top.mas_equalTo(SLMargin);
make.right.mas_equalTo(self.timeLabel.mas_left).mas_offset(-12);
}];
//时间
[self.contentView addSubview:self.timeLabel];
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-SLMargin);
make.centerY.mas_equalTo(self.titleLabel);
make.left.mas_greaterThanOrEqualTo(self.titleLabel.mas_right).mas_offset(12);
}];
[self.titleLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
[self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
[self.timeLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[self.timeLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
网友评论