更新 SnapKit 约束偏移量
Update SnapKit Constraint offset
我正在使用 SnapKit,但找不到更新约束偏移量的干净方法。这是一个简单的加载栏视图,其内部视图(expiredTime)必须根据百分比填充父视图(从左到右)。
我在自定义 UIView
的 awakeFromNib
中创建约束
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()
}
您的第一个解决方案不起作用,因为您在添加偏移量之前已经将 expiredTime 视图的 width
设置为全宽。要使其工作,您必须将宽度设置为 0
,然后添加偏移量。但是这里其实并不需要偏移量,你可以简单地将宽度设置为计算出的宽度:
override func updateConstraints() {
let width = self.frame.width * CGFloat(percentage)
self.expiredTime.snp.updateConstraints { (make) in
make.width.equalTo(width)
}
super.updateConstraints()
}
或 如果您保留对约束的引用,则根本不必覆盖 updateConstraints()
。您可以简单地调用约束的 update
方法(之后不调用 setNeedsUpdateConstraints()
)
constraintWidth?.update(offset: CGFloat(percentage) * view.bounds.width)
初始化的时候记得把宽度设置成0
constraintWidth
:
self.expiredTime.snp.makeConstraints { (make) in
make.left.top.bottom.equalTo(self)
self.constraintWidth = make.width.equalTo(0).constraint
}
我正在使用 SnapKit,但找不到更新约束偏移量的干净方法。这是一个简单的加载栏视图,其内部视图(expiredTime)必须根据百分比填充父视图(从左到右)。
我在自定义 UIView
的awakeFromNib
中创建约束
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()
}
您的第一个解决方案不起作用,因为您在添加偏移量之前已经将 expiredTime 视图的 width
设置为全宽。要使其工作,您必须将宽度设置为 0
,然后添加偏移量。但是这里其实并不需要偏移量,你可以简单地将宽度设置为计算出的宽度:
override func updateConstraints() {
let width = self.frame.width * CGFloat(percentage)
self.expiredTime.snp.updateConstraints { (make) in
make.width.equalTo(width)
}
super.updateConstraints()
}
或 如果您保留对约束的引用,则根本不必覆盖 updateConstraints()
。您可以简单地调用约束的 update
方法(之后不调用 setNeedsUpdateConstraints()
)
constraintWidth?.update(offset: CGFloat(percentage) * view.bounds.width)
初始化的时候记得把宽度设置成0
constraintWidth
:
self.expiredTime.snp.makeConstraints { (make) in
make.left.top.bottom.equalTo(self)
self.constraintWidth = make.width.equalTo(0).constraint
}