我正在制作一个自定义键盘,我有一个名为 setButtonConstraints() 的函数。当我将此函数插入 viewDidLoad() 并运行我的应用程序时,约束已正确设置。但是,当我将函数调用移动到 override func updateViewConstraints()(应该在 subview 布局后调用)时,没有设置任何约束。这是什么原因?
这是简单的 updateViewConstraints() 的样子:
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
setButtonConstraints()
}
请您参考如下方法:
根据 Apple 的文档,updateViewConstraints() 只有在约束需要 更新时才会被调用。此外,我相信您需要在更改约束后调用 super.updateViewConstraints() 。
以下链接很有帮助。
Where should I be setting autolayout constraints when creating views programmatically
