我正在尝试以特定事件发生后以编程方式更改viewDidLoad之外的按钮宽度(如果这很重要)。
我已将IB中的按钮宽度设置为等于视图宽度。
我为按钮宽度约束创建了一个IB插座:
@IBOutlet weak var imageButtonWidth: NSLayoutConstraint!
然后我试过了:
imageButtonWidth.constant = 0.49
view.setNeedsLayout()
,
imageButtonWidth.constant = 0.49
view.layoutIfNeeded()
和
imageButtonWidth.constant = 0.49
self.view.updateConstraints()
我在imageButtonWidth.constant = 0.49处设置了一个断点,它崩溃了应用程序,所以我知道它正在“运行”它只是没有正确激活。
我用过
imageButtonWidth.constant = 0.49
self.view.updateConstraints()
在viewdidload中对另一个应用程序没有任何问题,但似乎调用是在viewdidload之外,它将无法被确认。
任何建议都将不胜感激!
答案 0 :(得分:0)
正确的方法是使用
self.view.layoutIfNeeded()
更新约束但是你似乎连接了一个错误的插座,因为约束的值似乎很低。