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

Chr*_*ris 22 iphone debugging xcode

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

mar*_*rux 61

有一种转换视图层次结构的内置方法: recursiveDescription

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

它将输出如下内容:

<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'; […]
Run Code Online (Sandbox Code Playgroud)

请参阅:https://developer.apple.com/library/content/technotes/tn2239/_index.html

  • NSLog(@"%@",[view recursiveDescription]); (4认同)

Rob*_*ert 29

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

点击暂停按钮.

在此输入图像描述

在控制台中输入以下内容

po [[UIWindow keyWindow] recursiveDescription]
Run Code Online (Sandbox Code Playgroud)


Ben*_*tow 17

这些答案都是变体-recursiveDescription,但问题发布已经过去了大约三年,现在有更好的方法可以检查您的视图层次结构.-recursiveDescription对于大型层次结构总是有点难以管理 - 你花费更多时间查看输出日志而不是编程!

以下是一些较新的解决方案:

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

    DCIntrospect视图

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

    Spark Inspector主面板

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

    PonyDebugger Web View

  • Spark Inspector上的+1.允许我快速查明违规观点 (2认同)

Bra*_*son 10

根据Yannick的建议,Erica Sadun在这里有代码,可以打印视图层次结构(包含类和框架信息).您可以使用以下界面将其转换为UIView类别:

#import <UIKit/UIKit.h>

@interface UIView (ExploreViews)

- (void)exploreViewAtLevel:(int)level;

@end
Run Code Online (Sandbox Code Playgroud)

和实施:

#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
Run Code Online (Sandbox Code Playgroud)


mal*_*hal 5

试试这个魔法:

NSLog(@"%@", [self.view recursiveDescription]);
Run Code Online (Sandbox Code Playgroud)

  • 这有效,但我必须在 Xcode 5.1.1 中使用“performSelector:withObject:”;否则,这是一个错误。 (2认同)