xcode 我需要检查 iPhone 程序上的视图层次结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2343246/
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
I need to inspect the view hierarchy on an iPhone program
提问by Chris
How do I determine what the view structure on my iPhone program while running on the simulator?
在模拟器上运行时,如何确定 iPhone 程序上的视图结构?
采纳答案by Brad Larson
Along the lines of what Yannick suggests, Erica Sadun has code herethat pretty-prints the view hierarchy (with class and frame information). You can make this into a UIView category with the following interface:
按照 Yannick 的建议,Erica Sadun 在这里有代码可以漂亮地打印视图层次结构(带有类和框架信息)。您可以使用以下界面将其设置为 UIView 类别:
#import <UIKit/UIKit.h>
@interface UIView (ExploreViews)
- (void)exploreViewAtLevel:(int)level;
@end
and implementation:
和实施:
#import "UIView+ExploreViews.h"
void doLog(int level, id formatstring,...)
{
int i;
for (i = 0; i < level; i++) printf(" ");
va_list arglist;
if (formatstring)
{
va_start(arglist, formatstring);
id outstring = [[NSString alloc] initWithFormat:formatstring arguments:arglist];
fprintf(stderr, "%s\n", [outstring UTF8String]);
va_end(arglist);
}
}
@implementation UIView (ExploreViews)
- (void)exploreViewAtLevel:(int)level;
{
doLog(level, @"%@", [[self class] description]);
doLog(level, @"%@", NSStringFromCGRect([self frame]));
for (UIView *subview in [self subviews])
[subview exploreViewAtLevel:(level + 1)];
}
@end
回答by marcprux
There's a built-in way to dump the view hierarchy: recursiveDescription
有一种内置的方法可以转储视图层次结构: recursiveDescription
NSLog(@"%@", [view recursiveDescription]);
NSLog(@"%@", [view recursiveDescription]);
It will output something like this:
它将输出如下内容:
<UIView: 0x6a107c0; frame = (0 20; 320 460); autoresize = W+H; layer = […]
| <UIRoundedRectButton: 0x6a103e0; frame = (124 196; 72 37); opaque = NO; […]
| | <UIButtonLabel: 0x6a117b0; frame = (19 8; 34 21); text = 'Test'; […]
See: https://developer.apple.com/library/content/technotes/tn2239/_index.html
请参阅:https: //developer.apple.com/library/content/technotes/tn2239/_index.html
回答by Robert
You don't have to write a single log statement or alter your code at all.
您根本不必编写单个日志语句或更改代码。
Hit the pause button.
点击暂停按钮。
Enter the following into the console
在控制台输入以下内容
po [[UIWindow keyWindow] recursiveDescription]
回答by Ben Gotow
These answers are all variants of -recursiveDescription
, but it's been about three years since the question was posted and these days there are much better ways to inspect your view hierarchy. -recursiveDescription
was always a bit unmanageable with large hierarchies - you'd spend more time looking through your Output Log than you spent programming!
这些答案都是 的变体-recursiveDescription
,但问题发布已经大约三年了,现在有更好的方法来检查您的视图层次结构。-recursiveDescription
对于大型层次结构总是有点难以管理 - 您花在查看输出日志上的时间比花在编程上的时间还多!
Here are some newer solutions:
以下是一些较新的解决方案:
You can use DCIntrospectto print out the view hierarchy and view properties, and get some cool onscreen debugging markers.
You can use the Spark Inspectorto see your app's view hierarchy and interact with it in 3D. The Spark Inspector has a sidebar that mirrors Interface Builder and you can adjust your views while your app is running. (Full disclosure: I am the lead developer of this tool—send me your feature requests!)
You can use PonyDebuggerto connect the Chrome Inspector to your app - you can view your app's view hierarchy in the DOM view, and they also capture network traffic, which can be useful.
您可以使用DCIntrospect打印出视图层次结构和视图属性,并获得一些很酷的屏幕调试标记。
您可以使用Spark Inspector查看应用程序的视图层次结构并在 3D 中与其交互。Spark Inspector 有一个侧边栏,它反映了 Interface Builder,您可以在应用程序运行时调整视图。(完全披露:我是此工具的首席开发人员 - 将您的功能请求发送给我!)
您可以使用PonyDebugger将 Chrome Inspector 连接到您的应用程序 - 您可以在 DOM 视图中查看应用程序的视图层次结构,它们还捕获网络流量,这很有用。
回答by malhal
Try this magic:
试试这个魔法:
NSLog(@"%@", [self.view recursiveDescription]);
回答by Yannick Loriot
you can use some algo like this:
你可以使用一些这样的算法:
-(void)inspectView:(UIView *)aView level:(NSString *)level {
NSLog(@"Level:%@", level);
NSLog(@"View:%@", aView);
NSArray *arr = [aView subviews];
for (int i=0;i<[arr count];i++) {
[self inspectView:[arr objectAtIndex:i]
level:[NSString stringWithFormat:@"%@/%d", level, i]];
}
}
And add this line in an viewDidLoad for example:
并在 viewDidLoad 中添加这一行,例如:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self inspectView:self.viewControllers level:@""];
}
source: view hierarchy
来源:查看层次结构
回答by Lukas Petr
I've also dealt with this problem, and I came up with a single recursive method as a Category on UIView. It uses only basic Foundation classes, so it is not as complex as Erica's solution.
我也处理过这个问题,我想出了一个递归方法作为 UIView 上的类别。它只使用基本的 Foundation 类,因此它不像 Erica 的解决方案那么复杂。
Here is the Category method:
这是类别方法:
- (void)printSubviewsWithIndentation:(int)indentation {
NSArray *subviews = [self subviews];
for (int i = 0; i < [subviews count]; i++) {
UIView *currentSubview = [subviews objectAtIndex:i];
NSMutableString *currentViewDescription = [[NSMutableString alloc] init];
for (int j = 0; j <= indentation; j++) {
[currentViewDescription appendString:@" "]; // indent the description
}
// print whatever you want to know about the subview
[currentViewDescription appendFormat:@"[%d]: class: '%@'", i, NSStringFromClass([currentSubview class])];
// Log the description string to the console
NSLog(@"%@", currentViewDescription);
[currentViewDescription release];
// the 'recursiveness' nature of this method
[currentSubview printSubviewsWithIndentation:indentation+1];
}
}
For more detailed information, check out my blog post about this issue: http://www.glimsoft.com/01/07/how-to-inspect-subviews-hierarchy-of-any-uiview/
有关更多详细信息,请查看我关于此问题的博客文章:http: //www.glimsoft.com/01/07/how-to-inspect-subviews-hierarchy-of-any-uiview/