Swift三方集成&框架库

Swift :SnapKit 更新约束 & label宽度自适应

2020-08-10  本文已影响0人  Style_月月

创建约束

phoneNumLabel.snp.makeConstraints { (maker) in
    maker.left.equalToSuperview().offset(16)
    maker.top.equalToSuperview().offset(16)
    maker.height.equalTo(18)
    maker.width.equalTo(180)
}

更新约束

更新约束有两种方式

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自适应

phoneNumLabel.snp.makeConstraints { (maker) in
    maker.left.equalToSuperview().offset(16)
    maker.top.equalToSuperview().offset(16)
    maker.height.equalTo(18)
}
//设置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)
}
上一篇下一篇

猜你喜欢

热点阅读