在使用Masonry时候,有时候虽然界面显示正常,但是log里面会出现如下类似的警告。
一.问题描述:
[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.
[
<MASLayoutConstraint:0x600002e4db00 UIImageView:0x7f8510f5ec00.left == OTCMyHeaderView:0x7f8510d11da0.left + 14>,
<MASLayoutConstraint:0x600002e4db60 UIImageView:0x7f8510f5ec00.right == OTCMyHeaderView:0x7f8510d11da0.right - 14>,
<NSLayoutConstraint:0x600002962080 OTCMyHeaderView:0x7f8510d11da0.width == 0>
]
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x600002e4db60 UIImageView:0x7f8510f5ec00.right == OTCMyHeaderView:0x7f8510d11da0.right - 14>
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.
二. 解决方法:
1.通过断点找到有问题的控件XX
2. 初始化控件XX 下面 添加 代码 MASAttachKeys(控件XX), 可以传多个控件
3.run,看log
可以显示出控件的名字,一目了然就可以看到有问题的控件约束,修改即可。
布局太复杂, 不好确认是哪个空间, 还有一种方法
1.先点击Debug View Hierarchy
2.打印页控件层级和地址
OC使用
po [[UIWindow keyWindow] _autolayoutTrace]
Swift使用
expr -l objc++ -O -- [[UIWindow keyWindow] _autolayoutTrace]
3.搜索报错的地址0x111e168f0,定位到错误控件
断点小技巧:
链接:https://www.jianshu.com/p/838b012f12a0