iOS 定位处理约束冲突

一、添加断点定位

1、添加UIViewAlertForUnsatisfiableConstraints断点

  • 添加Symbolic Breakpoints
  • 编辑断点symbol填入
UIViewAlertForUnsatisfiableConstraints
  • 添加控制台打印action
po [[UIWindow keyWindow] _autolayoutTrace]

2、定位约束警告冲突

  • 添加好断点之后,当界面有约束冲突,就会触发断点,控制打印如下:
[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. 
(
    ,
    ,
    ,
    ,
)
 
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  may also be helpful.
  • 根据提示,找到约束有问题的控件地址0x7faf99f04010,然后全局搜索,就能找到具体是哪个控件
  • 如果控制台打印不够直观看出是哪个控件约束有问题,我们可以通过 LLDB命令工具chisel定位寻找。

二、Masonry Key 调试

冲突的autolayout 加上key后输出信息为:

make.right.and.bottom.equalTo(-20);
make.right.and.bottom.equalTo(-30).key(@"TestForKey");
"",
""

将类的地址换成了 加的key 这样的输出在调试时能很容易发现错误标记

MASAttachKeys(redView); 

三、lldb调试

此时我们只需要点击调试图层按钮,就可以进行命令执行了,不仅可以查看图层,打印视图信息,查看某个视图地址,还可以在 lldb 执行命令。

20210114111433963.png

根据系统要删除的约束的控件地址,

比如转化地址对象并输出属性

 (lldb) po ((UIView *)0x7fb43d20a6e0).subviews
<__NSArrayM 0x6000033baaf0>(
>
)

通过打印子视图就可以看见 view 下面包含Label控件,从而方便定位。

根据地址转对象,打印子视图层级
我们通过打印图层层级就可以很容易判断冲突控件属于哪个 View

po [((UIView *)0x7fa5382061c0)  recursiveDescription]

四、解决冲突

通常解决冲突的方法有:

  • 删除多余约束
  • 修改约束优先级

你可能感兴趣的:(iOS 定位处理约束冲突)