I think the linked answer is theoretically correct, but it's a bit unpractical when you actually want to make sense of the output. In the output as it is, you don't see any hierarchy, because you don't see which view is a child of which view. You should at least make the hierarchy a bit visible. For example:
some-view
another-view
whatever
foo
hello you
doesn't look so well. It's better when it looks like this:
(V:) some-view
(V:) another-view
(V:) whatever
(V:) foo
(V:) hello you
In the second snippet you see that "another-view" and "hello you" are both subviews of "some-view". And you can also see that logViewHierarchy has been called exactly once. In the first (hypothetical) debug output there is no way to know how often logViewHierarchy has been called. And with the "(V:)" in the output, you know which debug output lines are from a call to logViewHierarchy, and which debug output lines are from somewhere else! Its almost never a good idea to just log an object with NSLog(@"%@", obj);
; there should always be a log message at the beginning, so that you can search for it in the source code. (Most internet tutorials use NSLog
in a way that is totally ill-suited for a production environment, IMHO.)
// UIView+HierarchyLogging.m
@implementation UIView (ViewHierarchyLogging)
- (void)_logViewHierarchyWithIndent:(NSString *)indentation
{
NSLog(@"%@(V:) %@", indentation, self);
NSString *more_indentation = [indentation stringByAppendingString:@" "];
for(UIView *v in self.subviews) {
[v _logViewHierarchyWithIndent: more_indentation];
}
}
- (void)logViewHierarchy
{
[self _logViewHierarchyWithIndent:@""];
}
@end