创建约束
phoneNumLabel.snp.makeConstraints { (maker) in maker.left.equalToSuperview().offset(16) maker.top.equalToSuperview().offset(16) maker.height.equalTo(18) maker.width.equalTo(180) }
更新约束
更新约束有两种方式
- 在原有约束的基础上更新:更新top
phoneNumLabel.snp.makeConstraints { (maker) in maker.left.equalToSuperview().offset(100) }
- 重新设置约束
phoneNumLabel.snp.remakeConstraints { (maker) in maker.left.equalToSuperview().offset(50) maker.top.equalToSuperview().offset(50) maker.height.equalTo(30) maker.width.equalTo(100) }
label自适应
- 当只有一个label时,不设置宽度即可
phoneNumLabel.snp.makeConstraints { (maker) in maker.left.equalToSuperview().offset(16) maker.top.equalToSuperview().offset(16) maker.height.equalTo(18) }
- 当有两个label时,优先让其中一个宽度自适应
//设置phoneNumLabel的宽度优先自适应,关键代码!!! phoneNumLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 1000), for: .horizontal) phoneNumLabel.snp.makeConstraints { (maker) in maker.left.equalTo(callIconImage.snp_right).offset(16) maker.top.equalToSuperview().offset(16) maker.height.equalTo(18) } callIdentifier.snp.makeConstraints { (maker) in maker.left.equalTo(phoneNumLabel.snp_right).offset(8) maker.top.equalTo(16) maker.height.equalTo(16) }