两个约束高度冲突 UIView



我有UIView,我已经以编程方式配置了它。 我尝试更改多选输入视图(根视图(的高度,但没有更改,而是收到调试消息:

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 

最新更新