在日志中定位自动布局约束错误

Posted

技术标签:

【中文标题】在日志中定位自动布局约束错误【英文标题】:Locating auto layout constraints errors in the log 【发布时间】:2015-07-20 03:49:46 【问题描述】:

我的日志中出现了一堆自动布局约束错误,尽管我的情节提要中没有。我无法理解其中的大多数,尤其是定位引发错误的特定视图和文本字段:

2015-07-19 20:39:28.625 Daily[42447:417537] 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:0x7fd8a3e309e0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fd8a3dc4ea0(375)]>",
"<NSLayoutConstraint:0x7fd8a3dcc3f0 UIView:0x7fd8a3f416c0.leading == UIView:0x7fd8a3dc4ea0.leadingMargin>",
"<NSLayoutConstraint:0x7fd8a3dcc4e0 UIView:0x7fd8a3dc4ea0.centerX == UIView:0x7fd8a3f416c0.centerX>",
"<NSLayoutConstraint:0x7fd8a3dcc670 V:[UIView:0x7fd8a3f416c0(81)]>",
"<NSLayoutConstraint:0x7fd8a3dcc6c0 UIView:0x7fd8a3f416c0.width == 4.54321*UIView:0x7fd8a3f416c0.height>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fd8a3dcc6c0 UIView:0x7fd8a3f416c0.width == 4.54321*UIView:0x7fd8a3f416c0.height>
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-07-19 20:39:28.627 Daily[42447:417537] 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:0x7fd8a3e309e0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fd8a3dc4ea0(375)]>",
"<NSLayoutConstraint:0x7fd8a3dcbf20 V:[UIView:0x7fd8a3dc4a50]-(76)-[UIView:0x7fd8a3dcb8b0]>",
"<NSLayoutConstraint:0x7fd8a3dcbfc0 V:[_UILayoutGuide:0x7fd8a3dc4fb0]-(322)-[UIView:0x7fd8a3dcb8b0]>",
"<NSLayoutConstraint:0x7fd8a3dcc010 UIView:0x7fd8a3dc4ea0.width == 3.25203*UIView:0x7fd8a3dc4a50.height>",
"<NSLayoutConstraint:0x7fd8a3dcc100 V:[UIView:0x7fd8a3dcc150]-(34)-[UIView:0x7fd8a3dc4a50]>",
"<NSLayoutConstraint:0x7fd8a3dcc5d0 V:[_UILayoutGuide:0x7fd8a3dc4fb0]-(31)-[UIView:0x7fd8a3dcc150]>",
"<NSLayoutConstraint:0x7fd8a3dcdec0 H:[UIView:0x7fd8a3dcc150(368)]>",
"<NSLayoutConstraint:0x7fd8a3dcdf10 UIView:0x7fd8a3dcc150.width == 6.34483*UIView:0x7fd8a3dcc150.height>"
)

Will attempt to recover by breaking constraint 

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-07-19 20:39:28.628 Daily[42447:417537] 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:0x7fd8a3e309e0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fd8a3dc4ea0(375)]>",
"<NSLayoutConstraint:0x7fd8a3dcbed0 UIView:0x7fd8a3dcb8b0.trailing == UIView:0x7fd8a3dc4ea0.trailingMargin>",
"<NSLayoutConstraint:0x7fd8a3dcbf70 UIView:0x7fd8a3dcb8b0.leading == UIView:0x7fd8a3dc4ea0.leadingMargin>",
"<NSLayoutConstraint:0x7fd8a3dccf80 H:[UIView:0x7fd8a3dcb8b0(368)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fd8a3dccf80 H:[UIView:0x7fd8a3dcb8b0(368)]>

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-07-19 20:39:28.644 Daily[42447:417537] 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:0x7fd8a3e309e0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fd8a3dc4ea0(375)]>",
"<NSLayoutConstraint:0x7fd8a3dcc3f0 UIView:0x7fd8a3f416c0.leading == UIView:0x7fd8a3dc4ea0.leadingMargin>",
"<NSLayoutConstraint:0x7fd8a3dcc4e0 UIView:0x7fd8a3dc4ea0.centerX == UIView:0x7fd8a3f416c0.centerX>",
"<NSLayoutConstraint:0x7fd8a3dcc620 H:[UIView:0x7fd8a3f416c0(368)]>"
)

Will attempt to recover by breaking constraint 

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.

【问题讨论】:

您在故事板中使用尺寸等级吗?如果你换成一些更专业的尺寸,你有没有检查过任何东西?您是否正在移动/调整代码中的任何内容? 【参考方案1】:

您在视图中添加了错误的约束。如果您添加 centerX 约束,那么它们不需要添加前导空格约束,它将与约束冲突。将约束添加为 centerX、高度、宽度,并根据需要提供 Y 位置。

【讨论】:

【参考方案2】:

您的方法是可观的。但是,主要是我想通过禁用自动布局和类来开发一个应用程序。您需要在界面生成器中设置调整边距。如果您对我的解决方案不清楚,请告诉我,我会解释清楚:) :)

【讨论】:

是的,我并没有真正关注,我对 ios 开发还很陌生。我从 Swift 和 Auto Layout 开始,但我感觉 Auto Layout 并不像看起来那么容易。我问了其他一些人,其中一些人告诉我,我不能只为 UIView 设置动画,我需要更改约束以实现视图的动画。他们中的一些人告诉我一起放弃自动布局。所以我现在有点矛盾。 另外,如果你在***上查看我之前的question,你可以看到我遇到的问题。而且我几乎可以肯定,这个键盘和UIView动画的问题是由上面的Auto Layout约束中的错误引起的。 哦,那么您可以通过单击三角形图标在情节提要底部添加建议的约束。但是,我宁愿建议使用这种禁用方法,而且我没有遇到任何问题,我的应用程序可以在任何设备上以完全对齐的方式运行 :) 抱歉用问题轰炸你,但我也觉得我需要系统的知识或至少概述什么样的 Interface Builder(xib、storyboard、自动布局)适合什么情况。跨度> 禁用自动布局时如何布局视图?

以上是关于在日志中定位自动布局约束错误的主要内容,如果未能解决你的问题,请参考以下文章

自动布局约束不起作用

自动布局约束在生产中引发异常

UITableViewCell中的自动布局约束不匹配

ios 6自动布局约束错误

自动布局约束错误

自动布局约束错误