ios - 更改 UIView 约束高度带来其 subview 的约束警告

标签 ios swift layout

我有一个带有 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 具有 leadingtrailingtopbottom 以及一些 常量parentView

现在假设 10 个所有边 leadingtrailingtopbottom 当您将 height 设置为 0 时, subview 没有足够的空间来满足两侧顶部和底部的 10 值。所以它显示错误

尝试将低优先级设置为 topbottom,或者如果这不起作用,请将所有 subview 的高度设置为 > 0 低优先级

或者对 subview 使用 Stackview(垂直)并使用 spacing 而不是顶部和底部

希望你清楚为什么会发生这件事

关于ios - 更改 UIView 约束高度带来其 subview 的约束警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48415072/

相关文章:

ios - 如何在 UILabel 中将第一个字母大写?

ASP.NET 登录控件布局模板失败文本

ios - 滚动时未调用 sizeForItemAt

iphone - 知道 MFMailComposeViewController 何时消失

ios - 当用户切换应用程序的位置服务时,切换应用程序内 GPS UISwitch

ios - NSNotificationCenter defaultCenter iOS8。通知未传递给观察者之一

swift - 如何在MapView中绘制用户的路线

ios - 布局/方向更改后的 Cordova iPhone X 安全区域。

internet-explorer - CSS:两个 50% 的流体列不符合最小宽度

objective-c - 在 iOS 上,使用 Instruments,我们是否必须编译程序并运行它并停止它,然后在 Instrument 中按 Record 以查看使用的内存?