我需要检查iPhone程序上的视图层次结构

时间:2021-07-20 08:32:31

How do I determine what the view structure on my iPhone program while running on the simulator?

如何在模拟器上运行时确定iPhone程序的视图结构?

7 个解决方案

#1


10  

Along the lines of what Yannick suggests, Erica Sadun has code here that 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

#2


59  

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

#3


29  

You don't have to write a single log statement or alter your code at all.

您根本不必编写单个日志语句或更改代码。

Hit the pause button.

点击暂停按钮。

我需要检查iPhone程序上的视图层次结构

Enter the following into the console

在控制台中输入以下内容

po [[UIWindow keyWindow] recursiveDescription]

#4


17  

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 DCIntrospect to print out the view hierarchy and view properties, and get some cool onscreen debugging markers.

    您可以使用DCIntrospect打印出视图层次结构和视图属性,并获得一些很酷的屏幕调试标记。

    我需要检查iPhone程序上的视图层次结构

  • You can use the Spark Inspector to 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!)

    您可以使用Spark Inspector查看应用程序的视图层次结构并以3D方式与其进行交互。 Spark Inspector有一个侧边栏,可以镜像Interface Builder,您可以在应用程序运行时调整视图。 (完全披露:我是此工具的主要开发人员 - 向我发送您的功能请求!)

    我需要检查iPhone程序上的视图层次结构

  • You can use PonyDebugger to 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.

    您可以使用PonyDebugger将Chrome Inspector连接到您的应用程序 - 您可以在DOM视图中查看应用程序的视图层次结构,它们还可以捕获网络流量,这可能很有用。

    我需要检查iPhone程序上的视图层次结构

#5


5  

Try this magic:

试试这个魔法:

NSLog(@"%@", [self.view recursiveDescription]);

#6


3  

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

source:视图层次结构

#7


1  

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上的Category。它只使用基本的基础类,因此它不像Erica的解决方案那么复杂。

Here is the Category method:

这是Category方法:

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

#1


10  

Along the lines of what Yannick suggests, Erica Sadun has code here that 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

#2


59  

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

#3


29  

You don't have to write a single log statement or alter your code at all.

您根本不必编写单个日志语句或更改代码。

Hit the pause button.

点击暂停按钮。

我需要检查iPhone程序上的视图层次结构

Enter the following into the console

在控制台中输入以下内容

po [[UIWindow keyWindow] recursiveDescription]

#4


17  

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 DCIntrospect to print out the view hierarchy and view properties, and get some cool onscreen debugging markers.

    您可以使用DCIntrospect打印出视图层次结构和视图属性,并获得一些很酷的屏幕调试标记。

    我需要检查iPhone程序上的视图层次结构

  • You can use the Spark Inspector to 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!)

    您可以使用Spark Inspector查看应用程序的视图层次结构并以3D方式与其进行交互。 Spark Inspector有一个侧边栏,可以镜像Interface Builder,您可以在应用程序运行时调整视图。 (完全披露:我是此工具的主要开发人员 - 向我发送您的功能请求!)

    我需要检查iPhone程序上的视图层次结构

  • You can use PonyDebugger to 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.

    您可以使用PonyDebugger将Chrome Inspector连接到您的应用程序 - 您可以在DOM视图中查看应用程序的视图层次结构,它们还可以捕获网络流量,这可能很有用。

    我需要检查iPhone程序上的视图层次结构

#5


5  

Try this magic:

试试这个魔法:

NSLog(@"%@", [self.view recursiveDescription]);

#6


3  

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

source:视图层次结构

#7


1  

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上的Category。它只使用基本的基础类,因此它不像Erica的解决方案那么复杂。

Here is the Category method:

这是Category方法:

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