How to Inspect the View Hierarchy in iOS

How do I inspect the view hierarchy in iOS?

Xcode 6 now has 3D view hierarchy inspection built in like Reveal App and Spark Inspector.

iOS Sample Image 1

Click on the "Debug View Hierarchy" button while your app is running to pause execution and inspect the views at the current moment.

Sample Image

More info at Apple's documentation.

I need to inspect the view hierarchy on an iPhone program

Along the lines of what Yannick suggests, Erica Sadun has code here that pretty-prints the view hierarchy (with class and frame information). You can make this into a UIView category with the following interface:

#import <UIKit/UIKit.h>

@interface UIView (ExploreViews)

- (void)exploreViewAtLevel:(int)level;

@end

and implementation:

#import "UIView+ExploreViews.h"

void doLog(int level, id formatstring,...)
{
int i;
for (i = 0; i < level; i++) printf(" ");

va_list arglist;
if (formatstring)
{
va_start(arglist, formatstring);
id outstring = [[NSString alloc] initWithFormat:formatstring arguments:arglist];
fprintf(stderr, "%s\n", [outstring UTF8String]);
va_end(arglist);
}
}

@implementation UIView (ExploreViews)

- (void)exploreViewAtLevel:(int)level;
{
doLog(level, @"%@", [[self class] description]);
doLog(level, @"%@", NSStringFromCGRect([self frame]));
for (UIView *subview in [self subviews])
[subview exploreViewAtLevel:(level + 1)];
}

@end

How to find views in Debug View Hierarchy based on their address?

If you type (or copy/paste) the hex address into the search field at the bottom of the Navigator Pane, it should filter / find the object:

iOS Sample Image 2

Is there a way to see object names in debug view hierarchy?

No, there is not (unfortunately).

The information is not maintained at runtime to my knowledge, so you also won't be able to use lldb from Xcode's console to figure it out.

Probably worth a feature request to Apple!
https://feedbackassistant.apple.com/

View Debugging: What are the layers of view in any View Controller?

The Xcode debugger provides the ability to inspect and understand the view hierarchy.

With your app running in the debugger, click the Debug View Hierarchy button in the debug bar.
Sample Image

Here is reference of visual code debugging.
https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/debugging_with_xcode/
chapters/special_debugging_workflows.html

iOS Sample Image 3

Xcode Debug View Hierarchy finding viewcontroller

Update: Starting with Xcode 9, view controllers are listed right along the view hierarchy in the visual debugger.

You can get the memory address of the selected view from the inspector in Xcode and then use the console to get the view's view controller using -nextResponder.

http://developer.apple.com/library/ios/documentation/uikit/reference/UIResponder_Class/Reference/Reference.html#
//apple_ref/occ/instm/UIResponder/nextResponder

UIView implements this method by returning the UIViewController object
that manages it (if it has one) or its superview (if it doesn’t)



Related Topics



Leave a reply



Submit