Sometimes it’s useful to quickly see all the subviews of a UIView.
Perhaps you’re debugging a problem in one of your views or trying to understand the inner workings of one of the built in views.
You can simply iterate over a view’s subviews, but then you won’t see subviews deeper than one level. You need a method that recursively walks the hierarchy.
Luckily Apple has already done this with the recursiveDescription method, part of a UIDebugging category on UIView.
recursiveDescription was recently documented in a tech note titled iOS Debugging Magic.
At the gdb prompt in the Xcode debugger you can say:
(gdb) po [[self view] recursiveDescription]
and instantly see a description of the entire view hierarchy. We used this in our Transparent UIWebViews recipe to figure out the view hierarchy of a UIWebView:
(gdb) po [webView recursiveDescription] <UIWebView: 0x68220e0; frame = (0 0; 320 460); > | <UIScrollView: 0x4b2bee0; frame = (0 0; 320 460); > | | <UIImageView: 0x4b2dca0; frame = (0 0; 54 54); > | | <UIImageView: 0x4b2da20; frame = (0 0; 54 54); > | | <UIImageView: 0x4b2d9c0; frame = (0 0; 54 54); > | | <UIImageView: 0x4b12030; frame = (0 0; 54 54) > | | <UIImageView: 0x4b11fd0; frame = (-14.5 14.5; 30 1); > | | <UIImageView: 0x4b11f70; frame = (-14.5 14.5; 30 1); > | | <UIImageView: 0x4b11f10; frame = (0 0; 1 30); > | | <UIImageView: 0x4b11eb0; frame = (0 0; 1 30); > | | <UIImageView: 0x4b11e50; frame = (0 430; 320 30); > | | <UIImageView: 0x4b2d0c0; frame = (0 0; 320 30); > | | <UIWebBrowserView: 0x6005800; frame = (0 0; 320 460); >
Thanks!
I’ve been adding code to do the same to pretty much every iOS app I’ve written, but it’s a lot easier if it’s available standard…
Wow, this will be incredibly handy! Thanks!
Great post! We used to do this sort of thing all the time at Foundry376, so we developed an app called the Spark Inspector (http://www.sparkinspector.com/) to make things easier. It’s sort of like Instruments for your UIViews—you can explore your views while your app is running, change things to see what you need to do to fix your code, etc.. I’d love to get your thoughts on it. It’s shareware but I’d be happy to send you a free copy if you want to post about it on iDevRecipies.
Nice! You know, if you find yourself using -recursiveDescription a lot, you might want to check out http://www.sparkinspector.com/ or the iOS Hierarchy Viewer. Sometimes its hard to parse through the log output and they make it a lot easier to see what’s going on.