po [[UIWindow keyWindow] recursiveDescription] in LLDB to print the full view tree with frames and classes.Why this happens
Layout bugs in iOS apps often stem from views having incorrect frames, missing constraints, or being added to the wrong superview. Without a way to visualize the live view tree, developers resort to adding temporary background colors or writing assertion code—both slow and error-prone. Xcode includes built-in tools that let you pause a running app and examine every view’s class, frame, constraints, and containment relationship.
The two primary approaches are the graphical Debug View Hierarchy inspector (introduced in Xcode 6) and the LLDB command recursiveDescription on UIView. Both work on device and simulator, support UIKit and SwiftUI (via the underlying UIKit hosting), and require no code changes.
How to fix it
1. Use the Debug View Hierarchy button (recommended)
- Run your app on a simulator or connected device.
- Navigate to the screen with the layout issue.
- In Xcode’s debug bar, click the Debug View Hierarchy button (icon: three overlapping rectangles) or press ⌘⇧Y.
- Execution pauses. The canvas shows a 3D exploded view of the window’s view hierarchy.
- Use the sidebar to filter by class name, search for a view, or toggle constraints, clipped bounds, and layout margins.
- Click any view to see its size, position, auto-layout constraints, and the full ancestry chain in the inspectors.
- Resume execution with ⌘⌥Y or the Continue button.
Tip: Enable Show Clipped Content and Show Constraints in the canvas toolbar to spot views drawing outside their bounds or missing required constraints.
2. Print the hierarchy from LLDB with recursiveDescription
When you need a textual dump (e.g., for CI logs or quick comparison), pause in the debugger and run:
(lldb) po [[UIWindow keyWindow] recursiveDescription]
To inspect a specific view instead of the whole window:
(lldb) po [someView recursiveDescription]
Output example:
<UIWindow: 0x7f...; frame = (0 0; 393 852); ...>
| <UIView: 0x7f...; frame = (0 0; 393 852); ...>
| | <UINavigationBar: 0x7f...; frame = (0 0; 393 44); ...>
| | <UIView: 0x7f...; frame = (0 44; 393 808); ...>
| | | <UILabel: 0x7f...; frame = (20 20; 353 21); text = 'Hello'>
Each line shows the view’s class, address, frame, and key properties. Indentation reflects the superview/subview relationship.
3. Inspect SwiftUI views through the same tools
SwiftUI views are rendered by UIKit/AppKit hosting views. In Debug View Hierarchy you will see UIHostingController → UIHostingView → internal SwiftUI layout containers. The frames and constraints shown reflect the resolved layout. For deeper SwiftUI inspection, combine with the SwiftUI View Hierarchy inspector (Xcode 14+) which shows the declarative view tree alongside the rendered UIKit tree.
4. Capture a view hierarchy snapshot for later analysis
- In Debug View Hierarchy, choose Editor → Export View Hierarchy… (or right-click the canvas).
- Save the
.viewhierarchyfile. - Reopen it in Xcode anytime via File → Open… without the app running.
This is useful for sharing layout state with teammates or comparing before/after constraint changes.
How to prevent it
- Add Debug View Hierarchy to your standard debugging workflow; check it before filing layout bugs.
- Use accessibilityIdentifier on key views so they are searchable in the hierarchy sidebar.
- Enable View Debugging → Show Layout Constraints in the scheme’s Run options to see constraints live.
- Write UI tests that query the view hierarchy via
XCUIElementhierarchy to catch regressions automatically.
FAQ
Why does Debug View Hierarchy show only a blank window?
The app may be in a background state or the key window hasn’t been created yet. Ensure the target screen is visible and the app is in the foreground, then trigger the capture again.
Can I inspect view hierarchies on a physical device without Xcode attached?
No. Both Debug View Hierarchy and recursiveDescription require an active LLDB session. For field diagnostics, consider adding a debug overlay that dumps recursiveDescription to a file or console on a gesture.
Does recursiveDescription work on SwiftUI-only apps?
Yes. The root UIWindow still contains a UIHostingController hierarchy. The output will show the UIKit hosting layers; SwiftUI’s internal view tree is not exposed via this method.
Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.