怎么查找AutoLayout 问题?

423 阅读1分钟

有时候AutoLayout看起来没问题,但控制台打印了很多警告,
一定是哪里有问题。
日志里都是UIView,到底是哪个Cell里的哪个View出的问题?怎么找出来呢?

1、设置 UIViewAlertForUnsatisfiableConstraints 符号断点

2、用设备调试App,让App停在断点处

3、lldb 调试

3.1、调试找父视图

打印调用者 (lldb) po $x0

<MASLayoutConstraint:0x283151380 UIView:0x1092586f0.bottom == UIView:0x10924e000.bottom - 10>

打印第一个参数 (lldb) po ((UIView *)0x1092586f0)

<UIView: 0x1092586f0; frame = (0 0; 0 0); layer = <CALayer: 0x281450ee0>>

打印第一个参数的父视图 (lldb) po ((UIView *)0x1092586f0).superview

<UIView: 0x10924e000; frame = (0 0; 414 0); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x281a0b840>; layer = <CALayer: 0x281450ae0>>

打印第一个参数的父视图的父视图 (lldb) po ((UIView *)0x1092586f0).superview.superview

error: <user expression 3>:1:35: property 'superview' not found on object of type 'id'
((UIView *)0x1092586f0).superview.superview
Tips:lldb里需要增加类型

继续打印父视图的父视图 (lldb) po ((UIView *)(((UIView *)0x1092586f0).superview)).superview

<TTExampleStoreCell: 0x1092f0220; baseClass = UICollectionViewCell; frame = (0 1417; 414 0); layer = <CALayer: 0x281450a80>>

3.2、调试找accessibilityLabel,确定是哪个View。

给View添加accessibilityLabel很有必要。方便自动化测试外查找问题也比较容易

打印调用者 (lldb) po $x0

<MASLayoutConstraint:0x283151380 UIView:0x1092586f0.bottom == UIView:0x10924e000.bottom - 10>

打印第一个参数 (lldb) po ((UIView *)0x1092586f0)

导入UIKit (lldb) e @import UIKit;

打印第一个参数 (lldb) po ((UIView *)0x1092586f0).accessibilityLabel

TTTExampleCell-bgView 就确定了是哪个View

3.3、利用chisel打印视图树,直接搜索

安装brew upgrade chisel
然后根据提示把 command script import /usr/local/opt/chisel/libexec/fbchisellldb.py
添加到 ~/.lldbinit 文件里

看到日志输出后,暂停程序,在lldb里输入pviews,打印视图树。然后搜索,就能清楚的看出哪个View了

4、实在不行注释代码,肯定能找出问题来的