【问题标题】:"Unable to simultaneously satisfy constraints" with UITableViewCell“无法同时满足约束”与 UITableViewCell
【发布时间】:2021-12-12 18:09:03
【问题描述】:

我使用 Interface Builder 在 UITableViewCell 中为我的 Xcode 13.0/Swift 5/iOS 13.5 应用程序创建了一个非常简单的布局:

UIStackView(内部有 2 个UIViews 和UILabels)在顶部,UIView(内部有UIImageView)在其下方。

目前的具体限制是:

  • UIStackView:领先 10 点,尾随 10 点,顶部 0 点,底部 UIView 10 点,高度 50 点
  • UIView: 20pts 领先,20pts 尾随,10pts 底部,(从UIStackView 顶部 10pts),height=width
  • UIImageViewUIView 内:0pts 前/后/上/下,将中心 x 对齐到 Superview (=UIView)

与 stackoverflow 上有关此处警告的其他问题相反,我不直接设置 UITableViewCell 的高度,而是希望通过其内容设置它。 UIImageView 的图像在运行时设置并应保持其形状,因此方形图像应为方形,其他矩形图像应水平居中(-> 如有必要,左/右不可见条)而不是垂直居中。

Xcode 不会抱怨缺少约束,但这种布局总是给我臭名昭著的“无法同时满足约束”警告。

[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:0x600002d22d50 UIStackView:0x7fe4fa293330.height == 50   (active)>",
    "<NSLayoutConstraint:0x600002d22df0 UIView:0x7fe4fa29d1c0.height == UIView:0x7fe4fa29d1c0.width   (active)>",
    "<NSLayoutConstraint:0x600002d23070 V:|-(0)-[UIStackView:0x7fe4fa293330]   (active, names: '|':UITableViewCellContentView:0x7fe4fa2840f0 )>",
    "<NSLayoutConstraint:0x600002d23110 H:[UIView:0x7fe4fa29d1c0]-(20)-|   (active, names: '|':UITableViewCellContentView:0x7fe4fa2840f0 )>",
    "<NSLayoutConstraint:0x600002d23160 V:[UIStackView:0x7fe4fa293330]-(10)-[UIView:0x7fe4fa29d1c0]   (active)>",
    "<NSLayoutConstraint:0x600002d231b0 H:|-(20)-[UIView:0x7fe4fa29d1c0]   (active, names: '|':UITableViewCellContentView:0x7fe4fa2840f0 )>",
    "<NSLayoutConstraint:0x600002d23200 V:[UIView:0x7fe4fa29d1c0]-(10)-|   (active, names: '|':UITableViewCellContentView:0x7fe4fa2840f0 )>",
    "<NSLayoutConstraint:0x600002d36530 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe4fa2840f0.height == 325.5   (active)>",
    "<NSLayoutConstraint:0x600002d364e0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fe4fa2840f0.width == 295   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600002d22df0 UIView:0x7fe4fa29d1c0.height == UIView:0x7fe4fa29d1c0.width   (active)>

我已经检查过wtfautolayout.com,但我能看到的唯一错误是高度:如果宽度是 295,那么高度应该正好是 50+10+295-40+10=325(UIStackView.height + 中间约束 + UIView.width - 前导/尾随约束 + 底部约束),而不是 325.5。其他一切对我来说都很好,所以我不确定它为什么会计算出错误的高度(如果这甚至是问题所在)。

我已经尝试了几件事:

  • 添加了UIStackView 的固定高度(在一切正常之前没有使用它并且它没有抱怨它,但仍然收到警告)
  • UIView.width = UIImageView.height(1:1 纵横比,而不是上面那个)
  • UIImageView.width = UIImageView.height(1:1 纵横比,而不是上面那个)
  • 移除了“center x”约束

没有任何效果,我不断收到约束警告,如果由于我更改了某些内容而没有在纵向模式下显示,那么一旦我切换到横向模式,它就会出现。如何在 IB/无代码中解决此问题?

【问题讨论】:

    标签: ios uitableview autolayout


    【解决方案1】:

    尝试将 height=width 约束的优先级更改为 High 而不是 Required

    【讨论】:

    • 有趣,不再警告,谢谢。但为什么它会起作用?是否仍然存在隐藏的冲突,但我告诉它以不同的优先级忽略它?
    • 可能是这样。但这可能是暂时的冲突,只存在于某个时间点,而 ui 元素仍未渲染。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    相关资源
    最近更新 更多