NSLayoutConstraint 不是每次都更新
NSLayoutConstraint not updating every time
我正在使用 NSLayoutConstraint
进行测试,发现布局并非每次都更新。
这是我 ViewController
的 IB。
在这里,我选择了 IBAction
的 valueChange
的 Stepper
@IBAction func handleStepperValueChanged(_ sender: UIStepper) {
progressBar.progress = Float(sender.value / sender.maximumValue)
lblProgress.text = String(format: "%.2f %", progressBar.progress)
updateMyConstraint()
}
func updateMyConstraint() {
for constraints in lblProgress.constraints {
if constraints.firstAttribute == .width {
lblProgress.removeConstraint(constraints)
}
}
let multiplier = CGFloat(progressBar.progress)
let constLblWidth = NSLayoutConstraint(item: lblProgress, attribute: .width, relatedBy: .equal, toItem: progressBar, attribute: .width, multiplier: multiplier, constant: 0.0)
constLblWidth.priority = .defaultHigh
lblProgress.translatesAutoresizingMaskIntoConstraints = false
constLblWidth.isActive = true
lblProgress.updateConstraints()
}
这是我的输出。 (每次更新 Stepper 的值时都不会更新)
我是否缺少更新布局的内容?
请帮我解决这个问题。
每次更新 ProgressBar 标签也应该增加它的宽度。
我还尝试通过以下语句更新布局。
self.view.layoutIfNeeded()
self.view.layoutSubviews()
问题就在这里
lblProgress.removeConstraint(constraints)
应从 lblProgress
和 progressBar
的父项中删除约束
因为约束在他们之间
func updateMyConstraint() {
for constraints in self.view.constraints {
if constraints.identifier == "someIdentifier" && constraints.firstAttribute == .width {
print(constraints)
self.view.removeConstraint(constraints)
}
}
let multiplier = CGFloat(progressBar.progress)
let constLblWidth = NSLayoutConstraint(item: lbl, attribute: .width, relatedBy: .equal, toItem: progressBar, attribute: .width, multiplier: multiplier, constant: 0.0)
constLblWidth.priority = .required
lbl.translatesAutoresizingMaskIntoConstraints = false
constLblWidth.isActive = true
constLblWidth.identifier = "someIdentifier"
self.view.layoutIfNeeded()
}
constLblWidth.isActive = true
最初是将约束添加到 2 项之间的共享父项,在这种情况下为 self.view,因此当您删除约束时,您应该将其从 self.view 中删除你可以通过打印这里的约束来验证我编辑的 运行
我正在使用 NSLayoutConstraint
进行测试,发现布局并非每次都更新。
这是我 ViewController
的 IB。
IBAction
的 valueChange
的 Stepper
@IBAction func handleStepperValueChanged(_ sender: UIStepper) {
progressBar.progress = Float(sender.value / sender.maximumValue)
lblProgress.text = String(format: "%.2f %", progressBar.progress)
updateMyConstraint()
}
func updateMyConstraint() {
for constraints in lblProgress.constraints {
if constraints.firstAttribute == .width {
lblProgress.removeConstraint(constraints)
}
}
let multiplier = CGFloat(progressBar.progress)
let constLblWidth = NSLayoutConstraint(item: lblProgress, attribute: .width, relatedBy: .equal, toItem: progressBar, attribute: .width, multiplier: multiplier, constant: 0.0)
constLblWidth.priority = .defaultHigh
lblProgress.translatesAutoresizingMaskIntoConstraints = false
constLblWidth.isActive = true
lblProgress.updateConstraints()
}
这是我的输出。 (每次更新 Stepper 的值时都不会更新)
我是否缺少更新布局的内容?
请帮我解决这个问题。
每次更新 ProgressBar 标签也应该增加它的宽度。
我还尝试通过以下语句更新布局。
self.view.layoutIfNeeded()
self.view.layoutSubviews()
问题就在这里
lblProgress.removeConstraint(constraints)
应从 lblProgress
和 progressBar
的父项中删除约束
因为约束在他们之间
func updateMyConstraint() {
for constraints in self.view.constraints {
if constraints.identifier == "someIdentifier" && constraints.firstAttribute == .width {
print(constraints)
self.view.removeConstraint(constraints)
}
}
let multiplier = CGFloat(progressBar.progress)
let constLblWidth = NSLayoutConstraint(item: lbl, attribute: .width, relatedBy: .equal, toItem: progressBar, attribute: .width, multiplier: multiplier, constant: 0.0)
constLblWidth.priority = .required
lbl.translatesAutoresizingMaskIntoConstraints = false
constLblWidth.isActive = true
constLblWidth.identifier = "someIdentifier"
self.view.layoutIfNeeded()
}
constLblWidth.isActive = true
最初是将约束添加到 2 项之间的共享父项,在这种情况下为 self.view,因此当您删除约束时,您应该将其从 self.view 中删除你可以通过打印这里的约束来验证我编辑的 运行