[ios]打印ViewTree

查看当前view tree结构

下列代码加入到AppDelegate

- (void)dumpView:(UIView *)aView atIndent:(int)indent into:(NSMutableString *)outstring
{
    for (int i = 0; i < indent; i++) [outstring appendString:@"--"];
    [outstring appendFormat:@"[%2d] %@\n", indent, [[aView class] description]];
    for (UIView *view in [aView subviews])
        [self dumpView:view atIndent:indent + 1 into:outstring];
}

// Start the tree recursion at level 0 with the root view
- (NSString *) displayViews: (UIView *) aView
{
    NSMutableString *outstring = [[NSMutableString alloc] init];
    [self dumpView: self.window atIndent:0 into:outstring];
    return outstring ;
}
// Show the tree
- (void)logViewTreeForMainWindow
{
    //  CFShow([self displayViews: self.window]);
    NSLog(@"The view tree:\n%@", [self displayViews:self.window]);
}

然后在你想使用的地方使用

    AppDelegate *a=(AppDelegate *)[[UIApplicationsharedApplication] delegate];

    [a logViewTreeForMainWindow];

此时打印的就是 当前的view tree

如下:

[ 0] UIWindow
--[ 1] UILayoutContainerView
----[ 2] UITransitionView
------[ 3] UIViewControllerWrapperView
--------[ 4] UILayoutContainerView
----------[ 5] UINavigationTransitionView
------------[ 6] UIViewControllerWrapperView
--------------[ 7] UITableView
----------------[ 8] UITableViewCell
------------------[ 9] UITableViewCellContentView
--------------------[10] UILabel
--------------------[10] UIImageView
------------------[ 9] UIButton
--------------------[10] UIImageView
------------------[ 9] UIView
----------------[ 8] UITableViewCell
------------------[ 9] UITableViewCellContentView
--------------------[10] UILabel
--------------------[10] UIImageView
------------------[ 9] UIButton
--------------------[10] UIImageView
------------------[ 9] UIView
----------------[ 8] UITableViewCell
------------------[ 9] UITableViewCellContentView
--------------------[10] UILabel
--------------------[10] UIImageView
------------------[ 9] UIButton
--------------------[10] UIImageView
------------------[ 9] UIView
----------------[ 8] UITableViewCell
------------------[ 9] UITableViewCellContentView
--------------------[10] UILabel
--------------------[10] UIImageView
------------------[ 9] UIButton
--------------------[10] UIImageView
------------------[ 9] UIView
----------------[ 8] UITableViewCell
------------------[ 9] UITableViewCellContentView
--------------------[10] UILabel
--------------------[10] UIImageView
------------------[ 9] UIButton
--------------------[10] UIImageView
------------------[ 9] UIView
----------------[ 8] UITableViewCell
------------------[ 9] UITableViewCellContentView
--------------------[10] UILabel
--------------------[10] UIImageView
------------------[ 9] UIButton
--------------------[10] UIImageView
------------------[ 9] UIView
----------------[ 8] _UITableViewSeparatorView
----------------[ 8] _UITableViewSeparatorView
----------------[ 8] _UITableViewSeparatorView
----------------[ 8] _UITableViewSeparatorView
----------------[ 8] _UITableViewSeparatorView
----------------[ 8] _UITableViewSeparatorView
----------------[ 8] _UITableViewSeparatorView
----------[ 5] UINavigationBar
------------[ 6] UIMoreListController
------------[ 6] UINavigationItemView
------------[ 6] UINavigationButton
--------------[ 7] UIImageView
--------------[ 7] UIButtonLabel
----[ 2] UITabBarCustomizeView
------[ 3] UINavigationBar
--------[ 4] UINavigationBarBackground
--------[ 4] UINavigationItemView
--------[ 4] UINavigationButton
----------[ 5] UIImageView
----------[ 5] UIButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel
----[ 2] UITabBar
------[ 3] _UITabBarBackgroundView
------[ 3] UITabBarButton
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel
------[ 3] UITabBarButton
--------[ 4] UITabBarSelectionIndicatorView
--------[ 4] UITabBarSwappableImageView
--------[ 4] UITabBarButtonLabel

 

 

猜你喜欢

转载自poolo.iteye.com/blog/1833821