激活 segue 时自动布局无法满足约束

Auto layout unable to satisfy constraints when activating a segue

我有一个自定义 table 视图单元格,它带有自动布局建议的约束。在故事板中,所有约束都用蓝线标记,表示它们没有错误。 table 视图启动正常并且布局有效,但是当我点击单元格激活 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.

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

仅当我点击激活 segue 将用户带到下一个视图的单元格时,才会出现错误。当我点击后退按钮时,table视图 returns 但没有任何限制。

在 Attributes Inspector 中将 segue 设置为 Show(e.g. 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.

从错误日志来看,这两个相互冲突的约束似乎是图像视图的顶部约束和中心 Y 约束。只选一个--iOS 搞不清楚给图像视图的 y 坐标分配什么值。你想让image view离顶部一定距离,还是离superview垂直中心一定距离?

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

尝试将您的垂直约束优先级之一降低到 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 短的电池,它也使它静音。

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