我正在使用SnapKit,但找不到更新约束偏移的干净方法。这是一个简单的加载栏视图,其内部视图(expiredTime)必须根据百分比填充父级(从左到右)。
awakeFromNib
我在自定义 UIView中创建约束
self.expiredTime.snp.makeConstraints { (make) in
make.left.equalTo(self)
make.top.equalTo(self)
make.bottom.equalTo(self)
self.constraintWidth = make.width.equalTo(self).constraint
}
setNeedsUpdateConstraints()
然后每当更新时间时,我调用setNeedsUpdateConstraints()
这将触发默认的 updateConstraints() 已被苹果提示重载:
不起作用:(expiredTime 视图总是适合父视图)
override func updateConstraints() {
let offset = self.frame.width * CGFloat(percentage)
self.expiredTime.snp.updateConstraints { (make) in
make.width.equalTo(self).offset(offset).constraint
}
super.updateConstraints()
}
这也不起作用:
override func updateConstraints() {
let offset = self.frame.width * CGFloat(percentage)
self.constraintWidth?.update(offset: offset)
super.updateConstraints()
}
重建所有约束有效,但我想避免它
override func updateConstraints() {
self.expiredTime.snp.remakeConstraints() { (make) in
make.left.equalTo(self)
make.top.equalTo(self)
make.bottom.equalTo(self)
self.constraintWidth = make.width.equalTo(self).multipliedBy(self.percentage).constraint
}
super.updateConstraints()
}