两个约束高度冲突UIView

问题描述 投票:0回答:1

我有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
 }

为什么与自身冲突?

swift uiview constraints height
1个回答
0
投票

根据错误,您将高度设置两次-一次设置为常数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 
© www.soinside.com 2019 - 2024. All rights reserved.