Skip to content

Instantly share code, notes, and snippets.

@csknns
Last active August 29, 2015 14:14
Show Gist options
  • Save csknns/4a339fcd0839d9f4afbb to your computer and use it in GitHub Desktop.
Save csknns/4a339fcd0839d9f4afbb to your computer and use it in GitHub Desktop.
Print the subview hierarchy of a UIView
//
// UIView+PrintSubviewHierarchy.h
//
//
// Created by Christos Koninis on 2/2/15.
//
#import <UIKit/UIKit.h>
@interface UIView (PrintSubviewHierarchy)
- (void)printSubviewsWithIndentationLevel:(int)indentLevel;
@end
//
// UIView+PrintSubviewHierarchy.m
//
//
// Created by Christos Koninis on 2/2/15.
//
#import "UIView+PrintSubviewHierarchy.h"
@implementation UIView (PrintSubviewHierarchy)
- (void)printSubviewsWithIndentationLevel:(int)indentLevel {
NSArray *subviews = [self subviews];
for (UIView *currentSubview in [self subviews]) {
NSMutableString *currentViewDescription = [[NSMutableString alloc] init];
// Indent the actual description to provide visual clue of how deeply is the current view nested
for (int j = 0; j <= indentLevel; j++) {
[currentViewDescription appendString:@"\t"];
}
[currentViewDescription appendFormat:@"[%lu]: class: '%@'",
(unsigned long)[subviews indexOfObject:currentSubview],
NSStringFromClass([currentSubview class])];
NSLog(@"%@", currentViewDescription);
[currentSubview printSubviewsWithIndentationLevel:indentLevel+1];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment