【问题标题】:Auto layout unable to satisfy constraints when activating a segue激活 segue 时自动布局无法满足约束
【发布时间】:2025-11-25 12:00:02
【问题描述】:

我有一个自定义表格视图单元格,其中包含自动布局建议的约束。在情节提要中,所有约束都用蓝线标记,表示它们没有错误。 tableview 启动正常,布局工作,但只要我点击单元格激活 segue,我就会得到这个:

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.

我没有添加任何代码来为我的布局添加或删除约束。所有的约束都是由 Auto layout 自动添加的。

该错误仅在我点击激活 segue 以将用户带到下一个视图的单元格时出现。当我点击后退按钮时,tableview 返回但没有任何约束。

segue 在属性检查器中设置为 Show(例如 Push)。

这是消息的其余部分:

    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7ff1c276fec0 UIImageView:0x7ff1c2496b40.top == UITableViewCellContentView:0x7ff1c2496630.topMargin + 15>",
    "<NSLayoutConstraint:0x7ff1c276ff10 UIImageView:0x7ff1c2496b40.centerY == UITableViewCellContentView:0x7ff1c2496630.centerY>",
    "<NSLayoutConstraint:0x7ff1c24c9a70 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ff1c2496630(43.5)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ff1c276ff10 UIImageView:0x7ff1c2496b40.centerY == UITableViewCellContentView:0x7ff1c2496630.centerY>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-01-06 11:35:58.645 FitLift[4174:130902] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7ff1c2494c00 UIImageView:0x7ff1c277c070.top == UITableViewCellContentView:0x7ff1c277bfa0.topMargin + 15>",
    "<NSLayoutConstraint:0x7ff1c24a3360 UIImageView:0x7ff1c277c070.centerY == UITableViewCellContentView:0x7ff1c277bfa0.centerY>",
    "<NSLayoutConstraint:0x7ff1c24b7010 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ff1c277bfa0(43.5)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ff1c24a3360 UIImageView:0x7ff1c277c070.centerY == UITableViewCellContentView:0x7ff1c277bfa0.centerY>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-01-06 11:35:58.646 FitLift[4174:130902] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7ff1c24c4390 UIImageView:0x7ff1c24bc880.top == UITableViewCellContentView:0x7ff1c24bc3d0.topMargin + 15>",
    "<NSLayoutConstraint:0x7ff1c24c43e0 UIImageView:0x7ff1c24bc880.centerY == UITableViewCellContentView:0x7ff1c24bc3d0.centerY>",
    "<NSLayoutConstraint:0x7ff1c24c7880 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ff1c24bc3d0(43.5)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ff1c24c43e0 UIImageView:0x7ff1c24bc880.centerY == UITableViewCellContentView:0x7ff1c24bc3d0.centerY>

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

【问题讨论】:

  • 好吧。您已从该错误消息中删除了所有有用的信息,因此我们无能为力。粘贴到邮件的其余部分。

标签: ios xcode swift


【解决方案1】:

从错误日志来看,两个冲突的约束似乎是图像视图的顶部约束和 centerY 约束。只选择一个——iOS 很困惑为图像视图的 y 坐标分配什么值。您希望图像视图与顶部有一定距离,还是与超级视图的垂直中心有一定距离?

作为一般规则,如果您希望您的 UI 看起来像您想要的那样,请不要让 Xcode 为您自动添加约束。自己定义。我们在这里无法真正帮助您,因为我们不知道 Xcode 添加但不冲突的其他约束。

【讨论】:

  • 您好,感谢您的回复。我尝试删除图像视图上的所有约束并修复了错误但使 tableview 不可读。我找到了一种解决方法,但我只是不确定为什么这个错误只发生在 segue 期间。这里有一个类似的问题:*.com/questions/26355770/…,但它在 Obj-C 上,我没有经验。
  • 不,那家伙的问题不一样。您可能仍然遇到这些错误,因为您完全删除了所有约束,因此 iOS 可以看到其他视图的约束,但 imageView 没有任何约束,因此它只是将其放在随机位置。
【解决方案2】:

尝试将您的垂直约束优先级之一降低到 999 或更低。

"<NSLayoutConstraint:0x174091d00 V:|-(10)-[UILabel:0x14562e580'This']   (Names: '|':UITableViewCellContentView:0x17418d5b0 )>",
"<NSLayoutConstraint:0x174091e40 UITableViewCellContentView:0x17418d5b0.bottomMargin >= UILabel:0x14562e580'This'.bottom + 200>",
"<NSLayoutConstraint:0x17408c940 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x17418d5b0(43.5)]>"

我得到了这个,请注意最后一个,封装高度 43.5,我猜它是默认高度,如果您的垂直约束不适合该 43.5,则会引发错误。我仍然不确定自定尺寸单元是如何工作的,但降低优先级会使警告静音。我测试了一个比 43.5 短的电池,它也使它静音。

【讨论】:

    【解决方案3】:

    我遇到了类似的问题,通过设置 tableview 的estimatedRowHeight 解决了这个问题。默认的 43.5 甚至不足以覆盖我设置的固定点数(例如,我在单元格中的顶部标签距离单元格顶部 10 点,底部标签距离底部 10 点,以及行之间的空间标签等)

    【讨论】: