如何检查 iOS 中的视图层次结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5150186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I inspect the view hierarchy in iOS?
提问by Vaddadi Kartick
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.
是否有检查 iOS 应用程序视图层次结构的 GUI 工具?我正在考虑 Webkit 的网络检查器或类似工具。我希望调试布局问题,例如位置或大小错误的视图,或者子项未正确包含在其父项中。目前我必须添加断言来手动测试这些不同的条件,或者在不同的视图上设置不同的背景颜色,正如您想象的那样,这是一种非常乏味的方法。
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.
我查看了 Instruments 的UI recorder
,但它只记录和回放 UI 操作,并且无论如何只适用于 Mac 应用程序。
Is there a better solution?
有更好的解决方案吗?
采纳答案by Levi McCallum
Xcode 6 now has 3D view hierarchy inspection built in like Reveal App and Spark Inspector.
Xcode 6 现在内置了 3D 视图层次结构检查,如 Reveal App 和 Spark Inspector。
Click on the "Debug View Hierarchy" button while your app is running to pause execution and inspect the views at the current moment.
在您的应用程序运行时单击“调试视图层次结构”按钮以暂停执行并检查当前的视图。
More info at Apple's documentation.
Apple 文档中的更多信息。
回答by Simon Goldeen
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
不知道有没有GUI视图检查工具,但是我对UIView上的调试方法有一些运气: -recursiveDescription
if you pause the program in the debugger and input this into GDB (Edit: also works in LLDB)
如果您在调试器中暂停程序并将其输入 GDB(编辑:也适用于 LLDB)
po [[UIWindow keyWindow] recursiveDescription]
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 postwhich talked about this method and also linked to this helpful, but rather hard to find Apple tech note.
归功于这篇博客文章,其中讨论了这种方法,并且还链接到了这篇有用但很难找到的 Apple 技术说明。
回答by livingtech
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.
奇怪的是,现在还有另一种选择,http://revealapp.com/,截至本文时,它处于开放(免费)测试阶段。如您所见,它是另一个视觉检查器。
EDIT 2014-04-05: Reveal is out of Beta and no longer free. There is a 30-day trial, however.
编辑 2014-04-05:Reveal 已退出 Beta 版,不再免费。但是,有 30 天的试用期。
回答by Damian Ko?akowski
This question is old but let me put info here about new tool which I develop:
https://github.com/glock45/iOS-Hierarchy-Viewer
这个问题很旧,但让我在此处提供有关我开发的新工具的信息:https:
//github.com/glock45/iOS-Hierarchy-Viewer
回答by livingtech
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.
只是为了保持这个线程是最新的,我最近一直在玩Spark Inspector。它不是免费的,但非常好。
回答by Scott Bossak
The FLEX Debuggerprovides an in app view inspector that allows you to modify the UI in a running app. It also logs network requests.
该FLEX调试器中的应用程序视图检查,使您可以修改正在运行的应用程序的UI提供了一个。它还记录网络请求。
https://github.com/Flipboard/FLEX
回答by AVEbrahimi
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.
商业版:http: //revealapp.com/ 我测试了revealapp 的测试版,虽然有bug,但还是不错的。另一个商业工具:http: //sparkinspector.com/它可以无缝运行。
回答by Vlad
Swift 4.
斯威夫特 4.
iOS:
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()
用法(在调试器中): po myView.dump()
回答by Alix
This dumps all in debug window.(Hard to read tho) :( Working on iOS 10, Xcode 8.3.3
这会在调试窗口中转储所有内容。(难以阅读):( 在 iOS 10 上工作,Xcode 8.3.3
po UIApplication.shared.keyWindow?.recursiveDescription()
回答by mph
The approved answer no longer works for me, using Xcode 8and Swift 2.3. Here's what does work for me:
已批准的答案不再适用于我,使用Xcode 8和 Swift 2.3。这里是做什么工作对我来说:
po UIApplication.sharedApplication().keyWindow?.recursiveDescription()
po UIApplication.sharedApplication().keyWindow?.recursiveDescription()