173

Is there a GUI tool that inspects the view hierarchy of an iOS app? I'm thinking about Webkit's web inspector or similar tools. I'm looking to debug layout issues, like views having the wrong position or size, or a child not being properly contained in its parent. Currently I have to add asserts that test these various conditions by hand, or set different background colours on different views, and as you can imagine, that's a really tedious way to go about it.

I looked at Instruments's UI recorder, but that only records and plays back UI actions` and, in any case, works only for Mac apps.

Is there a better solution?

Kartick Vaddadi
  • 4,265
  • 5
  • 33
  • 45

11 Answers11

264

I don't know if there is a GUI view inspection tool, but I have had some luck with the debugging method on UIView: -recursiveDescription

if you pause the program in the debugger and input this into GDB (Edit: also works in LLDB)

po [[UIWindow keyWindow] recursiveDescription]

You get a printout of your entire view hierarchy. You can also call it on a specific view to get a printout of the view hierarchy of that view.

It can be a little tedious to wade through the info you get out of it, but it has proved useful to me.

Credit goes to this blog post which talked about this method and also linked to this helpful, but rather hard to find Apple tech note.

Pang
  • 8,605
  • 144
  • 77
  • 113
Simon Goldeen
  • 8,990
  • 3
  • 32
  • 43
  • 1
    I've got: error: cannot find interface declaration for '$__lldb_objc_class' any ideas? (lldb) po [[UIWindow keyWindow] recursiveDescription] error: cannot find interface declaration for '$__lldb_objc_class' error: cannot find interface declaration for '$__lldb_objc_class' error: 2 errors parsing expression – Zennichimaro Apr 19 '13 at 03:34
  • 1
    I believe this answer is no longer the canonical answer, as there are now GUI tools available that directly answer the question 'Is there a GUI tool...?' – theraven Jul 31 '13 at 03:18
  • starting with xcode 5.1 I started getting this error whenever I type recursiveDescription error: instance method 'undoManager' has incompatible result types in different translation units ('id' vs. 'NSUndoManager *') note: instance method 'undoManager' also declared here error: 1 errors parsing expression – abbood Jul 21 '14 at 11:46
  • 1
    While the question is old, and asks about graphical methods, this specific answer relates to textual solutions. A discussion of the canonical in-code solution can be found e.g. here: https://stackoverflow.com/a/14193682/2431627. i.e. using the _printHierarchy property via KVC. – Robin Macharg Mar 12 '18 at 09:01
172

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

enter image description here

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

enter image description here

More info at Apple's documentation.

jameshfisher
  • 26,641
  • 22
  • 94
  • 145
Levi McCallum
  • 4,458
  • 4
  • 28
  • 35
  • 5
    If the view debugging option is not available, see http://stackoverflow.com/questions/24040322/xcode-5-6-view-debugging-always-grayed-out. – kennytm Dec 25 '14 at 09:36
  • At first, I didn't see the toolbar shown by Levi McCallum because it is at the very bottom of the Xcode window if the app doesn't have any debug output. And once the button is pushed, the View Hierarchy window was blank until I pushed one of the buttons at the bottom, or adjusted one of the sliders. – Carl Smith Sep 04 '15 at 04:18
  • Once you start relying on this tool it always craps out. I don't know why or how to fix it but it seems to happen on more complicated views. – Departamento B Oct 07 '16 at 21:57
46

Reveal app screenshot

Oddly enough, now there is another option, http://revealapp.com/, which as of this post is in an open (free) beta. As you can see it's another visual inspector.

EDIT 2014-04-05: Reveal is out of Beta and no longer free. There is a 30-day trial, however.

livingtech
  • 3,381
  • 28
  • 40
  • 1
    I've tried a few tools for this (Spark Inspector, iOS Hierarchy Viewer and the firebug-like view in the Frank testing tool). So far Reveal is my favourite. – Stew Jul 31 '13 at 02:17
  • 1
    But TOTALLY still worth it. Way better than the built in Xcode tools for viewing views – Will Larche Dec 31 '14 at 14:52
35

This question is old but let me put info here about new tool which I develop: https://github.com/glock45/iOS-Hierarchy-Viewer enter image description here

Damian Kołakowski
  • 2,713
  • 20
  • 25
  • is it possible to refresh the page automatically when I navigate to another view in the simulator? furthermore, is it possible to change values in this viewer(so I can do some tests on the browser without writing real code)? thanks :) – Brian Nov 05 '13 at 08:18
  • 1
    @Brian thanks Brain. I started to prototype possibility to change values. It's turned off in official release. Stay tuned ! – Damian Kołakowski Nov 05 '13 at 18:11
  • Thanks a lot for the suggestion. I managed to solve my issue with this tool, a issue that couldn't resolve using Xcode's UI debugger! – D6mi Jul 05 '16 at 09:20
  • Thanks, Damian, but for people reading this, this tool seems unmaintained. – Kartick Vaddadi Apr 20 '17 at 03:54
17

enter image description here

Just to keep this thread up to date, I've been recently playing with Spark Inspector. It's not free, but it's very nice.

Ben Gotow
  • 14,276
  • 3
  • 39
  • 47
livingtech
  • 3,381
  • 28
  • 40
8

The FLEX Debugger provides an in app view inspector that allows you to modify the UI in a running app. It also logs network requests.

https://github.com/Flipboard/FLEX

enter image description here

Scott Bossak
  • 2,431
  • 18
  • 17
6

Free : Just type this in inspector :

po [[UIWindow keyWindow] recursiveDescription]

Commercial : http://revealapp.com/ I tested beta version of revealapp, it was good though has bugs. Another Commercial tool : http://sparkinspector.com/ it's working seamless.

AVEbrahimi
  • 13,285
  • 12
  • 70
  • 151
6

Swift 4.

iOS:

extension UIView {

   // Prints results of internal Apple API method `recursiveDescription` to console.
   public func dump() {
      Swift.print(perform(Selector(("recursiveDescription"))))
   }
}

macOS:

extension NSView {

   // Prints results of internal Apple API method `_subtreeDescription` to console.
   public func dump() {
      Swift.print(perform(Selector(("_subtreeDescription"))))
   }
}

Usage (in debugger): po myView.dump()

Vlad
  • 4,733
  • 1
  • 48
  • 58
2

This dumps all in debug window.(Hard to read tho) :( Working on iOS 10, Xcode 8.3.3

po UIApplication.shared.keyWindow?.recursiveDescription()
Alix
  • 2,687
  • 3
  • 23
  • 41
0

The approved answer no longer works for me, using Xcode 8 and Swift 2.3. Here's what does work for me:

po UIApplication.sharedApplication().keyWindow?.recursiveDescription()

mph
  • 651
  • 1
  • 8
  • 21
  • 1
    Thanks, mph, but this doesn't work either in Swift 3: error: property 'shared' not found on object of type 'UIApplication' – Kartick Vaddadi Feb 09 '17 at 02:01
  • Swift 4 and Xcode 9.1 complain with "error: :3:32: error: cannot call value of non-function type 'UIApplication' UIApplication.sharedApplication().keyWindow?.recursiveDescription()" – Jason Harrison Jan 08 '18 at 20:40
0

For swift/Xcode 10, enter this into the debug console:

po yourView.value(forKey: "recursiveDescription")!

It will print out a recursive hierarchy for any given UIView.

(Credit: How to debug your view hierarchy using recursiveDescription)

Albert Bori
  • 9,324
  • 9
  • 47
  • 76