Skip to content

Instantly share code, notes, and snippets.

@Kalgar
Created March 24, 2011 04:37
Show Gist options
  • Save Kalgar/884588 to your computer and use it in GitHub Desktop.
Save Kalgar/884588 to your computer and use it in GitHub Desktop.
iOS function for dumping view hierarchy
***************************************************************************************************
DISCLAIMER:
I did not write this, I found it on Stack Overflow attributed to user Sophtware. Finding it useful, I decided to save this here, in case SO deleted the page (since the question was closed as a non-question)
***************************************************************************************************
void dumpViews(UIView* view, NSString *text, NSString *indent)
{
Class cl = [view class];
NSString *classDescription = [cl description];
while ([cl superclass])
{
cl = [cl superclass];
classDescription = [classDescription stringByAppendingFormat:@":%@", [cl description]];
}
if ([text compare:@""] == NSOrderedSame)
NSLog(@"%@ %@", classDescription, NSStringFromCGRect(view.frame));
else
NSLog(@"%@ %@ %@", text, classDescription, NSStringFromCGRect(view.frame));
for (NSUInteger i = 0; i < [view.subviews count]; i++)
{
UIView *subView = [view.subviews objectAtIndex:i];
NSString *newIndent = [[NSString alloc] initWithFormat:@" %@", indent];
NSString *msg = [[NSString alloc] initWithFormat:@"%@%d:", newIndent, i];
dumpViews(subView, msg, newIndent);
[msg release];
[newIndent release];
}
}
***************************************************************************************************
How to Use
***************************************************************************************************
To dump the entire view hierarchy of your application, call the method like this:
dumpViews([[UIApplication sharedApplication] keyWindow], @"", @"");
To display the hierarchy of the the camera view, override this method in your controller:
navigationController:willShowViewController:viewController:animated
and call the dump routine like this:
dumpViews(self.modalViewController.view, @"", @"");
For all other views:
dumpViews(myView, @"", @"");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment