前言
在做項目的時候希柿,無意間看到自動布局約束警告,開始也也覺得沒什么养筒,雖然有警告狡汉,但并不影響UI展示效果。但是越來越有代碼潔癖的我闽颇,也忍受不了控制臺輸出一大堆約束警告Log盾戴,于是就查閱如何定位解決約束沖突,同時自己也記錄下來兵多。下面開始介紹具體操作步驟吧尖啡。
一橄仆、添加UIViewAlertForUnsatisfiableConstraints斷點
- 添加Symbolic Breakpoints
屏幕快照 2018-06-04 下午2.44.35.png - 編輯斷點symbol填入
UIViewAlertForUnsatisfiableConstraints
- 添加控制臺打印action
po [[UIWindow keyWindow] _autolayoutTrace]
屏幕快照 2018-06-04 下午2.44.50.png
二、定位約束警告沖突
- 添加好斷點之后衅斩,當界面有約束沖突盆顾,就會觸發(fā)斷點,控制打印如下:
[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:0x604000ab04a0 UIButton:0x7faf99f04010.width == 40>,
<MASLayoutConstraint:0x604000ab66e0 UIButton:0x7faf99f04010.left == CYBButtonView:0x7faf99f83360.left + 10>,
<MASLayoutConstraint:0x604000abaa00 UILabel:0x7faf99f5f8e0.left == UIButton:0x7faf99f04010.right>,
<MASLayoutConstraint:0x604000abd580 UILabel:0x7faf99f5f8e0.left == CYBButtonView:0x7faf99f83360.left + 15>,
)
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x604000ab04a0 UIButton:0x7faf99f04010.width == 40>
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.
- 根據(jù)提示畏梆,找到約束有問題的控件地址0x7faf99f04010您宪,然后全局搜索,就能找到具體是哪個控件
- 如果控制臺打印不夠直觀看出是哪個控件約束有問題奠涌,我們可以通過 LLDB命令工具chisel定位尋找宪巨。
三、解決沖突
通常解決沖突的方法有:
- 刪除多余約束
- 修改約束優(yōu)先級