【问题标题】:Unable to simultaneously satisfy constraints. Probably at least one of the constraints无法同时满足约束。可能至少有一个约束
【发布时间】:2021-01-28 21:28:13
【问题描述】:

我正在使用 swift 5 和故事板做一个 ios 聊天应用程序,我的一个视图包含一个表格视图和单元格,但是我遇到了这种错误,我不明白它到底是什么以及我该如何解决它!

显然,每当调用 tableView.reload() 函数时,就会发生此问题。

这是我的观点

view storyboard tableview

如图所示,有一个 tableview,在 tableCell 中,有一个 view 包含 label 来显示用户名,右边是一个 image view 来显示用户照片

2020-10-14 14:59:17.638363+0200 Chatiw[17620:455807] [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:0x6000000e7840 UILabel:0x7fafbe739be0.width >= 261   (active)>",
    "<NSLayoutConstraint:0x6000000e7930 UIImageView:0x7fafbe73a4d0.width == 62   (active)>",
    "<NSLayoutConstraint:0x6000000e7b10 UILabel:0x7fafbe739be0.leading >= UITableViewCellContentView:0x7fafbe739850.leadingMargin   (active)>",
    "<NSLayoutConstraint:0x6000000e7c50 H:[UILabel:0x7fafbe739be0]-(51)-[UIImageView:0x7fafbe73a4d0]   (active)>",
    "<NSLayoutConstraint:0x6000000e7ca0 UIImageView:0x7fafbe73a4d0.trailing == UITableViewCellContentView:0x7fafbe739850.trailingMargin   (active)>",
    "<NSLayoutConstraint:0x600000096580 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fafbe739850.width == 375   (active)>",
    "<NSLayoutConstraint:0x6000000e7e80 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x600001a9cb60'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':UITableViewCellContentView:0x7fafbe739850 )>",
    "<NSLayoutConstraint:0x6000000e7f20 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001a9cb60'UIViewLayoutMarginsGuide']-(16)-|(LTR)   (active, names: '|':UITableViewCellContentView:0x7fafbe739850 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000000e7840 UILabel:0x7fafbe739be0.width >= 261   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-14 14:59:17.642314+0200 Chatiw[17620:455807] [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:0x600000096710 UILabel:0x7fafbe43e0f0.width >= 261   (active)>",
    "<NSLayoutConstraint:0x6000000968f0 UIImageView:0x7fafbe43e360.width == 62   (active)>",
    "<NSLayoutConstraint:0x6000000ea620 UILabel:0x7fafbe43e0f0.leading >= UITableViewCellContentView:0x7fafbe43dd60.leadingMargin   (active)>",
    "<NSLayoutConstraint:0x6000000eb8e0 H:[UILabel:0x7fafbe43e0f0]-(51)-[UIImageView:0x7fafbe43e360]   (active)>",
    "<NSLayoutConstraint:0x6000000eb7f0 UIImageView:0x7fafbe43e360.trailing == UITableViewCellContentView:0x7fafbe43dd60.trailingMargin   (active)>",
    "<NSLayoutConstraint:0x600000097250 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fafbe43dd60.width == 375   (active)>",
    "<NSLayoutConstraint:0x6000000eb9d0 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x600001a9a5a0'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':UITableViewCellContentView:0x7fafbe43dd60 )>",
    "<NSLayoutConstraint:0x6000000eac60 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001a9a5a0'UIViewLayoutMarginsGuide']-(16)-|(LTR)   (active, names: '|':UITableViewCellContentView:0x7fafbe43dd60 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000096710 UILabel:0x7fafbe43e0f0.width >= 261   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-14 14:59:17.644909+0200 Chatiw[17620:455807] [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:0x6000000eb930 UILabel:0x7fafc084b7c0.width >= 261   (active)>",
    "<NSLayoutConstraint:0x6000000ea8f0 UIImageView:0x7fafc084bb60.width == 62   (active)>",
    "<NSLayoutConstraint:0x6000000eab20 UILabel:0x7fafc084b7c0.leading >= UITableViewCellContentView:0x7fafc084b630.leadingMargin   (active)>",
    "<NSLayoutConstraint:0x6000000ea8a0 H:[UILabel:0x7fafc084b7c0]-(51)-[UIImageView:0x7fafc084bb60]   (active)>",
    "<NSLayoutConstraint:0x6000000ea990 UIImageView:0x7fafc084bb60.trailing == UITableViewCellContentView:0x7fafc084b630.trailingMargin   (active)>",
    "<NSLayoutConstraint:0x6000000cf700 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fafc084b630.width == 375   (active)>",
    "<NSLayoutConstraint:0x6000000d3250 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x600001a9a760'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':UITableViewCellContentView:0x7fafc084b630 )>",
    "<NSLayoutConstraint:0x6000000d2df0 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001a9a760'UIViewLayoutMarginsGuide']-(16)-|(LTR)   (active, names: '|':UITableViewCellContentView:0x7fafc084b630 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000000eb930 UILabel:0x7fafc084b7c0.width >= 261   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-14 14:59:17.647864+0200 Chatiw[17620:455807] [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:0x6000000efb60 UILabel:0x7fafc0d0a3d0.width >= 261   (active)>",
    "<NSLayoutConstraint:0x6000000d2da0 UIImageView:0x7fafc084c430.width == 62   (active)>",
    "<NSLayoutConstraint:0x6000000d3b60 UILabel:0x7fafc0d0a3d0.leading >= UITableViewCellContentView:0x7fafc0d09aa0.leadingMargin   (active)>",
    "<NSLayoutConstraint:0x6000000d3840 H:[UILabel:0x7fafc0d0a3d0]-(51)-[UIImageView:0x7fafc084c430]   (active)>",
    "<NSLayoutConstraint:0x6000000d37a0 UIImageView:0x7fafc084c430.trailing == UITableViewCellContentView:0x7fafc0d09aa0.trailingMargin   (active)>",
    "<NSLayoutConstraint:0x6000000eac10 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fafc0d09aa0.width == 375   (active)>",
    "<NSLayoutConstraint:0x6000000d05f0 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x600001a9a840'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':UITableViewCellContentView:0x7fafc0d09aa0 )>",
    "<NSLayoutConstraint:0x6000000d3f20 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001a9a840'UIViewLayoutMarginsGuide']-(16)-|(LTR)   (active, names: '|':UITableViewCellContentView:0x7fafc0d09aa0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000000efb60 UILabel:0x7fafc0d0a3d0.width >= 261   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-14 14:59:17.650367+0200 Chatiw[17620:455807] [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:0x6000000978e0 UILabel:0x7fafbe442740.width >= 261   (active)>",
    "<NSLayoutConstraint:0x6000000979d0 UIImageView:0x7fafbe4429b0.width == 62   (active)>",
    "<NSLayoutConstraint:0x600000097bb0 UILabel:0x7fafbe442740.leading >= UITableViewCellContentView:0x7fafbe4425b0.leadingMargin   (active)>",
    "<NSLayoutConstraint:0x600000097cf0 H:[UILabel:0x7fafbe442740]-(51)-[UIImageView:0x7fafbe4429b0]   (active)>",
    "<NSLayoutConstraint:0x600000097d40 UIImageView:0x7fafbe4429b0.trailing == UITableViewCellContentView:0x7fafbe4425b0.trailingMargin   (active)>",
    "<NSLayoutConstraint:0x60000009c780 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fafbe4425b0.width == 375   (active)>",
    "<NSLayoutConstraint:0x600000097f20 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x600001ade840'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':UITableViewCellContentView:0x7fafbe4425b0 )>",
    "<NSLayoutConstraint:0x6000000efc00 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001ade840'UIViewLayoutMarginsGuide']-(16)-|(LTR)   (active, names: '|':UITableViewCellContentView:0x7fafbe4425b0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000000978e0 UILabel:0x7fafbe442740.width >= 261   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

请任何人都可以向我解释这个问题到底是什么,以便我理解它以及如何解决它?我的所有表格视图都存在这个问题!

【问题讨论】:

  • 如果您没有任何 UI 问题,可以忽略此消息。如果你这样做了,并且你的 UI 出现了问题,那意味着你有一些不能同时满足的约束,并且这些约束正在被打破,所以其他的可以得到满足。这通常意味着您以相互冲突的方式设置约束,但除非我们看到您的 UI,否则无法说明如何设置。
  • 您是否曾经准确地阅读过错误信息并尝试过信息提示的内容?
  • @vadian 我是 swift 新手,如果我确实理解错误,我不会问我的朋友 :)
  • @Adis 我确实在我的 UI 中添加了一张照片

标签: ios swift storyboard swift5


【解决方案1】:

解决这些问题或警告非常困难。

这里有一个关键提示。

您实际上可以为每个人添加一个名称!约束。

这样做。

使名称清晰明了,例如“第二个框的顶部约束”或其他任何内容。

您不必执行应用程序中的每一个约束,只需执行相关区域中可能存在问题的约束即可。

首先,您必须这样做。 :/

请注意,此站点上有大量关于此的讨论,有 100 多个问题,例如 https://stackoverflow.com/a/31833436/294884

本质上,“你的约束有一个小问题”,特别是你可能有“某个地方的约束太多”(或者更微妙的是,你必须改变一个的优先级)。

您的第一步是命名技巧,至少要找出 Apple 所指的地狱限制:/


有些人建议这个顺便说一句! http://wtfautolayout.com


对于这个特定的问题:

在这种特殊情况下,在很多很多可能的问题中,原来 OP 的问题是“两个约束”问题:

如果您确实有/需要两个约束,那么其中一个必须具有与另一个不同的优先级。

【讨论】:

  • 您好,非常感谢,正如我在我的问题中明确提到的那样,我不想要只是如何解决我想清楚地了解我做错了什么,所以下次我知道如何处理它,无论如何我尝试了你的方法,它似乎工作但我有一个问题。你给我一个问题,当我将优先级从(1000)更改为(999)时,错误消失了,但为什么?这意味着我降低了我告诉 xcode 我不关心这个约束或什么的优先级?
  • 啊,确切的答案是在某个地方你有两个约束,很可能你只需要一个。如果您确实有/需要两个约束,是的,那么其中一个必须具有不同的优先级!
  • 非常感谢你,现在我知道发生了什么:) 再次感谢你
  • 是的,这是一个非常非常困难的问题。 iOS 中经常存在关于约束的细微错误。 (这是一个..stackoverflow.com/q/43322973/294884)我个人对约束有很多疑问。这很难。
【解决方案2】:

如果没有 UI 示例(ViewController 和 Cell),这有点难以回答。但是似乎TableViewCell子视图的约束不符合单元格的实际宽度。

您为标签和图像查看距离和大小设置了比屏幕空间更大的宽度限制。

一个可能对你有用的例子: [-(leading=0)-(label)-(

这样在imageView和label之间会有最少50个点的约束...

几个建议:

  1. 无需在标签上设置宽度约束 - 它应该从其中的实际文本推断,只需描述(带约束)到其他视图的距离。

  2. 根本不需要在 imageView 上设置宽度/高度约束,并查看 contentMode 属性以了解如何调整其中的图像。

  3. 除非您想将子视图在单元格内居中,否则无需将前导/尾随约束设置为 >= /

如果标签可能包含长字符串,另一个提示是将其numberOfLines 属性设置为0。 并设置self.tableView.rowHeight = UITableViewAutomaticDimension

See this awesome explanation about TableViewCell constraints

【讨论】:

  • 非常感谢您的回答,我已将照片添加到我的视图和约束中,请您检查一下并添加任何有用的东西吗?
  • 是的,所以您需要决定要删除哪个约束,看起来系统会以非常好的方式为您做出决定。我建议删除标签上的宽度限制(宽度 >=260)。并将 label.trailing - avatar.leading 设置为一个灵活的,例如 >=50 (您可以在其中设置它们之间的最小距离......请参阅 261(最小标签宽度)+ 51(图像和标签之间的水平间距)+ 62 (图像宽度)+ 16(边距)+ 16(边距)总和为 = 402,大于 375(屏幕/单元格宽度)
  • 你也可以像@Fattie建议的那样设置优先级,这将是最简单的解决方案
  • 但请注意,在许多情况下,您确实需要两个约束!它是灵活布局的基础。这很棘手。
猜你喜欢
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 2020-11-15
  • 1970-01-01
  • 2014-10-27
  • 2014-06-19
相关资源
最近更新 更多