简单递归实现遍历子视图

1、方法实现

1
2
3
4
5
6
7
8
9
10
11
12
13
/// 遍历子视图
- (void)subviewWithBlock:(void(^)(UIView *view))aBlock
{
????// 如果有子视图则进入循环
????for (UIView *view in self.subviews) {
????????// 如果子视图不属于UIView则退出本次循环
????????if(![view isKindOfClass:[UIView class]]) continue;
????????// Block不为空则执行Block
????????if(aBlock) aBlock(view);
????????// 通过递归遍历子视图的子视图
????????[view subviewWithBlock:aBlock];
????}
}

2、方法调用

1
2
3
[self subviewWithBlock:^(UIView *view) {
????// Code...
}];

Leave a Reply