Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple recursive function for printing a view hierarchy in cocoa touch. Originally from user207616 on Stack Overflow.
// --------------------------------------------------
#import <UIKit/UIKit.h>
@interface UIView (Hierarchy)
- (void)printViewHierarchy;
// --------------------------------------------------
#import "UIView+Hierarchy.h"
@implementation UIView (Hierarchy)
- (void)printViewHierarchy
[self _logView:self index:0];
#pragma mark - Private
- (void)_logView:(UIView*)v index:(int)i
NSMutableString *str = [NSMutableString string];
for (int u = 0; u<i; u++) {
[str appendString:@"| "];
[str appendFormat:@"<%@ %p frame:%@>", v.class, v, NSStringFromCGRect(v.frame)];
// of course you can change it to display your accessibility hint/label
printf("%s\n", [str UTF8String]);
for (UIView *vv in v.subviews) {
[self _logView:vv index:i+1];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.