iOS遍历视图里的所有子视图

// Recursively travel down the view tree, increasing the indentation level for children
- (void)dumpView:(UIView *)aView atIndent:(int)indent into:(NSMutableString *)outstring
{
    for (int i = 0; i < indent; i++) [outstring appendString:@"--"];
    aView.backgroundColor = [UIColor clearColor];
    [outstring appendFormat:@"[%2d] %@==%@\n", indent, [[aView class] description],aView.backgroundColor];
    
    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: aView atIndent:0 into:outstring];
    return outstring;
}
// Show the tree
- (void)logViewTreeForMainWindow: (UIView *) aView
{
    //  CFShow([self displayViews: self.window]);
    NSLog(@"The view tree:\n%@", [self displayViews:aView]);
}


你可能感兴趣的:(ios,打印所有子视图)