Swift 编程自动布局约束问题

Issue with Swift programmatic auto-layout constraint

我只是在使用

toolBar.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true 

用于设置我的 topAnchor。

但是,当我将常量从 0 更改为实际数字时,控制台会给我

[LayoutConstraints] Unable to simultaneously satisfy constraints.

这个错误。 我在 viewDidLoad 中调用此约束,但将其更改为 viewWillLayoutSubviews 或其他部分无法解决问题。

如何修复此错误? 谢谢!

因为你创建了另一个,你需要编辑当前的

var topCon:NSLayoutConstraint!

topcon = toolBar.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0)

topCon.isActive = true

然后玩常量

topCon.constant = // anyNumber

我只需要将 translatesAutoresizingMaskIntoConstraints = false