gpt4 book ai didi

swift - 改变 NSLayoutConstraint

转载 作者:行者123 更新时间:2023-11-28 11:36:24 28 4
gpt4 key购买 nike

我想改变 ScrollView 的宽度。首先,我这样做了:

var scrollWidthConstraint: NSLayoutConstraint!

然后设置它:

scrollWidthConstraint = contentView.widthAnchor.constraint(equalToConstant: CGFloat(scrollWidth()))
scrollWidthConstraint.isActive = true

然后我想把它改成这样:

  scrollWidthConstraint = contentView.widthAnchor.constraint(equalToConstant: width)
scrollWidthConstraint.isActive = true
contentView.setNeedsLayout()

但是看起来我没有删除旧的约束,我在日志中看到:

"<NSLayoutConstraint:0x6000031d7160 UIView:0x7fc92c808120.width == 1333   (active)>",
"<NSLayoutConstraint:0x6000031fcff0 UIView:0x7fc92c808120.width == 933.1 (active)>"

如何删除旧约束并更新它?

最佳答案

这个

scrollWidthConstraint = contentView.widthAnchor.constraint(equalToConstant: width)
scrollWidthConstraint.isActive = true
contentView.setNeedsLayout()

无论您将其重新分配给旧的约束,都会创建另一个约束,您需要

scrollWidthConstraint.constant = width
view.layoutIfNeeded()

关于swift - 改变 NSLayoutConstraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55263739/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com