使用递归方法获取某个视图的所有子视图

使用递归方法获取某个子视图的所有子视图,并有层次的打印出来。

代码示例

// 子视图
UIView *view0 = [[UIView alloc] initWithFrame:CGRectMake(10.0, 10.0, 10.0, 10.0)];
[self.view addSubview:view0];
//
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10.0, 40.0, 100.0, 60.0)];
[self.view addSubview:view1];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 40.0, 30.0)];
[view1 addSubview:label];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(70.0, 30.0, 30.0, 30.0)];
[view1 addSubview:imageview];
UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 30.0, 70.0, 30.0)];
[view1 addSubview:textfield];
//
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(10.0, 60.0, 100.0, 100.0)];
[self.view addSubview:view2];
UIImageView *view21 = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 10.0, 80.0, 80.0)];
[view2 addSubview:view21];
UILabel *view22 = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, 60.0, 60.0)];
[view21 addSubview:view22];
UIButton *view23 = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 10.0, 40.0, 40.0)];
[view22 addSubview:view23];
UITextView *view24 = [[UITextView alloc] initWithFrame:CGRectMake(10.0, 10.0, 20.0, 20.0)];
[view23 addSubview:view24];


// 递归获取子视图(level从1开始)
- (void)showSubview:(UIView *)view level:(int)level
{
    for (UIView *subview in view.subviews) {
        // 根据层级决定前面空格个数,来缩进显示
        NSString *blank = @"";
        for (int i = 1; i < level; i++)
        {
            blank = [NSString stringWithFormat:@"  %@", blank];
        }
        // 打印子视图类名
        NSLog(@"%@%d: %@", blank, level, subview.class);
        if ([subview isKindOfClass:[UITextField class]] || [subview isKindOfClass:[UITextView class]]) {
            NSLog(@"%@%d: %@", blank, level, @(YES));
        }
        // 递归获取此视图的子视图
        [self showSubview:subview level:(level + 1)];
    }
}

- (void)click
{
    // 打印所有子视图
//    [self showSubview:self.navigationController.navigationBar level:1];
    [self showSubview:self.view level:1];
}

打印结果

2018-06-05 23:06:40.146491+0800 DemoSubviews[3608:297071] 1: UIView
2018-06-05 23:06:40.146863+0800 DemoSubviews[3608:297071] 1: UIView
2018-06-05 23:06:40.147185+0800 DemoSubviews[3608:297071]   2: UILabel
2018-06-05 23:06:40.147382+0800 DemoSubviews[3608:297071]   2: UIImageView
2018-06-05 23:06:40.147709+0800 DemoSubviews[3608:297071]   2: UITextField
2018-06-05 23:06:40.147975+0800 DemoSubviews[3608:297071]   2: 1
2018-06-05 23:06:40.148235+0800 DemoSubviews[3608:297071]     3: _UITextFieldContentView
2018-06-05 23:06:40.148628+0800 DemoSubviews[3608:297071] 1: UIView
2018-06-05 23:06:40.148874+0800 DemoSubviews[3608:297071]   2: UIImageView
2018-06-05 23:06:40.149139+0800 DemoSubviews[3608:297071]     3: UILabel
2018-06-05 23:06:40.149345+0800 DemoSubviews[3608:297071]       4: UIButton
2018-06-05 23:06:40.149696+0800 DemoSubviews[3608:297071]         5: UITextView
2018-06-05 23:06:40.150062+0800 DemoSubviews[3608:297071]         5: 1
2018-06-05 23:06:40.150556+0800 DemoSubviews[3608:297071]           6: _UITextContainerView
2018-06-05 23:06:40.150916+0800 DemoSubviews[3608:297071]           6: UIImageView
2018-06-05 23:06:40.151277+0800 DemoSubviews[3608:297071]           6: UIImageView

猜你喜欢

转载自blog.csdn.net/potato512/article/details/80588729