两个约束高度冲突的UIView

Two constraint height conflict UIView

我有 UIView,我以编程方式对其进行了配置。 我试图更改 MultiSelectsInputView(根视图)的高度,但我没有更改,而是收到调试消息:

MainApp[5765:768019] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x28204d360 MainApp.MultiSelectsInputView:0x159c08830.height == 300   (active)>",
    "<NSLayoutConstraint:0x2820880a0 MainApp.MultiSelectsInputView:0x159c08830.height == 275   (active)>"
)

我正在设置这样的限制条件

 func constraintHeight(constant: CGFloat) {
        translatesAutoresizingMaskIntoConstraints = false
        heightAnchor.constraint(equalToConstant: constant).isActive = true
 }

为什么会和自己冲突?

根据错误,您设置了两次高度 - 一次使用常量值 300,另一次使用值 275,并且这两个都同时处于活动状态。这两个约束相互冲突,因此一次只能激活一个。

查看代码,您似乎用不同的值调用了 constraintHeight() 方法两次。如果您需要更改视图的高度,您应该首先停用旧的高度限制。

保留对高度限制的引用,以便您以后可以激活/停用它们。

var heightConstraint: NSLayoutConstraint?

heightConstraint = view.heightAnchor.constraint(equalToConstant: constant)
// To activate or deactivate toggle the boolean isActive property
heightConstraint?.isActive = true // Activate height constraint
heightConstraint?.isActive = false // Deactivate height constraint