// 并排的两个label,优先让右边的label宽度自适应
// 左边的label
let leftLabel = UILabel()
self.view.addSubview(leftLabel)
leftLabel.backgroundColor = UIColor.lightGray
leftLabel.text =
"这是左边的label"
leftLabel.font = UIFont.systemFont(ofSize: 12)
// 右边的label
let rightLabel = UILabel()
self.view.addSubview(rightLabel)
rightLabel.backgroundColor = UIColor.orange
rightLabel.text =
"这是右边的label,优先宽度自适应内容"
rightLabel.font = UIFont.systemFont(ofSize: 12)
// 别扯我,谢谢(这就是右边label优先宽度自适应的关键代码)
rightLabel.setContentHuggingPriority(1000,
for
: UILayoutConstraintAxis.horizontal);
leftLabel.snp.makeConstraints { (make)
in
make.left.equalToSuperview()
make.bottom.equalTo(-20)
make.height.equalTo(20)
}
rightLabel.snp.makeConstraints { (make)
in
make.left.equalTo(leftLabel.snp.right)
make.bottom.height.equalTo(leftLabel)
make.right.equalToSuperview()
}
请发表评论