Debugging Layouts with Recursive View Descriptions in Xcode

If you haven’t yet, go watch the Debugging talk from CocoaHeads by Mike Hay for some great Xcode debugging tips. If you didn’t know about things like the exception breakpoint you’ll be much happier.

One item Mike discussed was using UIView’s recursiveDescription method in the LLDB debugger to see the view hierarchy of the current screen. This is a great way to debug your view layouts but typing po [[[UIApplication sharedApplication] keyWindow] recursiveDescription] into the debugger is tiresome.

Instead, try this quick tip:

Create an .lldbinit file in your home directory (~/.lldbinit) and add the following:

command regex rd 's/^[[:space:]]*$/po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]/' 's/^(.+)$/po [%1 recursiveDescription]/'

Now debugging the recursive description is a easy as pausing and entering rd in the debugger.

(lldb) rd
$0 = 0x0a498210 <UIWindow: 0xa57e5b0; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0xa57b720>>
   | <UILayoutContainerView: 0xc0b3360; frame = (20 0; 748 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0xc0b3420>>
   |    | <UILayoutContainerView: 0xc0b4010; frame = (321 0; 703 748); clipsToBounds = YES; layer = <CALayer: 0xc0b4080>>
…etc…

For more fine grained control set a breakpoint in a view controller controller and use rd self.view:

(lldb) rd self.view

Now go fix those layouts!