我有一个带有 subview 的 UIView parentView
。所有约束都设置正确。
当我想通过heightConstraint.constant = 0
隐藏此parentView
并通过heightConstraint.constant = 200
显示它时。我会收到如下警告:
[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.
我能得到它是因为 subviews
约束不满足新的高度。
那么这是否可以通过更改 heightConstraint
并同时满足 subview 约束来实现?
(我可以通过parentView.isHidden
来实现,只是不想用这种方式)
最佳答案
这是因为您的 subview 具有 leading
、trailing
、top
和 bottom
以及一些 常量
到parentView
现在假设 10 个所有边 leading
、trailing
、top
和 bottom
当您将 height
设置为 0 时, subview 没有足够的空间来满足两侧顶部和底部的 10 值。所以它显示错误
尝试将低优先级设置为 top
和 bottom
,或者如果这不起作用,请将所有 subview 的高度设置为 > 0 低优先级
或者对 subview 使用 Stackview(垂直)并使用 spacing
而不是顶部和底部
希望你清楚为什么会发生这件事
关于ios - 更改 UIView 约束高度带来其 subview 的约束警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48415072/