iOS:转储视图和约束的层次结构树
Posted
技术标签:
【中文标题】iOS:转储视图和约束的层次结构树【英文标题】:iOS: Dumping hierarchy tree of views and constraints 【发布时间】:2016-11-14 11:37:47 【问题描述】:在 ios 中调试自动布局可能很烦人。我们正在寻找一种方法来转储子视图及其约束,以便进行更好的调试。
有两种有用的方法可以使用调试器获取一些信息:
转储视图树:po [[UIWindow keyWindow] recursiveDescription]
跟踪约束:po [[UIWindow keyWindow] _autolayoutTrace];
*** 中有几个答案,其中包含转储视图的想法但没有相应的约束。有什么有用的方法吗?
【问题讨论】:
【参考方案1】:以下函数可用于很好地转储视图及其相应的自动布局约束的树:
void dumpViewRecursivelyWithConstraints(UIView *vw, NSString *title)
if (!vw)
vw = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
void (^dumpViewRecursively)(UIView*, NSString*, NSMutableString*); //local var definition
void (^ __block __weak weakDumpViewRecursively)(UIView*, NSString*, NSMutableString*); //weak copy for recursion
weakDumpViewRecursively = dumpViewRecursively = ^void(UIView *vw, NSString* gap, NSMutableString *mStr)
[mStr appendFormat:@"%@.%@\n", gap, vw];
for (NSLayoutConstraint *c in vw.constraints)
NSString *cStr = [NSString stringWithFormat:@"%@", c];
NSInteger space = [cStr rangeOfString:@" "].location; //skipping up to space for a neater output
[mStr appendFormat:@"%@| %@\n", gap, [cStr substringFromIndex:space+1]];
[mStr appendFormat:@"%@`----------------------------\n", gap];
NSString *nextGap = [gap stringByAppendingString:@" "];
NSArray *subs = [vw subviews];
for (int i=0; i<[subs count]; i++)
weakDumpViewRecursively(subs[i], nextGap, mStr); //call myself recursively
;
NSMutableString *mStr = [[NSMutableString alloc] init];
[mStr appendFormat:@"\n\n************** %@: **************\n", title];
dumpViewRecursively(vw, @"", mStr);
NSLog(@"%@", mStr);
输出如下:
.<UIView: 0x12c5bcb10; frame = (0 45; 1024 703); autoresize = W+H; gestureRecognizers = <NSArray: 0x12e436c70>; layer = <CALayer: 0x12c5b8580>>
| UIScrollView:0x12d176a00.width == UIView:0x12c5bcb10.width>
| UIScrollView:0x12d176a00.height == UIView:0x12c5bcb10.height>
| UIScrollView:0x12d176a00.centerX == UIView:0x12c5bcb10.centerX>
| UIScrollView:0x12d176a00.centerY == UIView:0x12c5bcb10.centerY>
| PlayerView:0x12c5bda20.width == UIView:0x12c5bcb10.width>
`----------------------------
.<UIScrollView: 0x12d176a00; frame = (0 0; 1024 703); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x12e18b6e0>; layer = <CALayer: 0x12e054ec0>; contentOffset: 0, 0; contentSize: 1848, 703>
| V:|-(0)-[UIView:0x12c5d1860] (Names: '|':UIScrollView:0x12d176a00 )>
| H:|-(0)-[UIView:0x12c5d1860](LTR) (Names: '|':UIScrollView:0x12d176a00 )>
| UIView:0x12c5d1860.height == UIScrollView:0x12d176a00.height>
`----------------------------
.<UIView: 0x12c5d1860; frame = (0 0; 1848 703); layer = <CALayer: 0x12c5ad540>>
| H:[UIView:0x12c5d1860(1848)]>
| H:|-(0)-[PlayerView:0x12c5bda20](LTR) (Names: '|':UIView:0x12c5d1860 )>
| V:|-(0)-[PlayerView:0x12c5bda20] (Names: '|':UIView:0x12c5d1860 )>
| PlayerView:0x12c5bda20.height == UIView:0x12c5d1860.height>
| UIView:0x12e02bf50.top == UIView:0x12c5d1860.centerY - 10>
【讨论】:
以上是关于iOS:转储视图和约束的层次结构树的主要内容,如果未能解决你的问题,请参考以下文章
利用递归层次遍历句法结构树(Stanfordcorenlp及nltk)