iOS递归打印UIView结构
1、iOS在lldb调试窗口可用recursiveDescription打印该UIView的整个结构,如下:
2、用Objective-C代码实现如下:
+ (NSString *)showViewHierarchy:(UIView *)view level:(NSInteger)level
{
NSMutableString * description = [NSMutableString string];
NSMutableString * indent = [NSMutableString string];
for (NSInteger i = 0; i < level; i++)
{
[indent appendString:@" |"];
}
[description appendFormat:@"\n%@%@", indent, [view description]];
for (UIView * item in view.subviews)
{
[description appendFormat:@"%@", [UIView showViewHierarchy:item level:level + 1]];
}
return [description copy];
}
//
// 实现view的循环打印
- (NSString *)recursiveDiscription
{
return [UIView showViewHierarchy:self level:0];
}
本文介绍了一种在 iOS 开发中使用 Objective-C 实现的 UIView 结构递归打印方法。通过此方法,开发者可以在调试时方便地查看视图层级结构,帮助理解界面布局。

1万+

被折叠的 条评论
为什么被折叠?



