How do I inspect the view hierarchy in iOS?

IosCocoa TouchUikit

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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.

Solution 3 - Ios

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.

Solution 4 - Ios

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

Solution 5 - Ios

enter image description here

Just to keep this thread up to date, I've been recently playing with http://sparkinspector.com/">Spark Inspector. It's not free, but it's very nice.

Solution 6 - Ios

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

Solution 7 - Ios

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.

Solution 8 - Ios

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()

Solution 9 - Ios

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

po UIApplication.shared.keyWindow?.recursiveDescription()

Solution 10 - Ios

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)

Solution 11 - Ios

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()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKartick VaddadiView Question on Stackoverflow
Solution 1 - IosSimon GoldeenView Answer on Stackoverflow
Solution 2 - IosLevi McCallumView Answer on Stackoverflow
Solution 3 - IoslivingtechView Answer on Stackoverflow
Solution 4 - IosDamian KołakowskiView Answer on Stackoverflow
Solution 5 - IoslivingtechView Answer on Stackoverflow
Solution 6 - IosScott BossakView Answer on Stackoverflow
Solution 7 - IosAVEbrahimiView Answer on Stackoverflow
Solution 8 - IosVladView Answer on Stackoverflow
Solution 9 - IosAlixView Answer on Stackoverflow
Solution 10 - IosAlbert BoriView Answer on Stackoverflow
Solution 11 - IosmphView Answer on Stackoverflow