Created
January 10, 2013 15:15
-
-
Save modamoda/4502797 to your computer and use it in GitHub Desktop.
[iOS] UIView category for finding sub-views
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIView+SubView.h | |
// | |
// Created by moda | |
// Copyright (c) 2012년 moda. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIView (SubView) | |
- (void)displaySubViews; | |
- (UIView *)findSubView:(NSString *)className; | |
- (NSArray *)findViewsInDescendent:(NSString *)className; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIView+SubView.m | |
// | |
// Created by moda | |
// Copyright (c) 2012년 moda. All rights reserved. | |
// | |
#import "UIView+SubView.h" | |
@implementation UIView (SubView) | |
- (void)displaySubViews | |
{ | |
[self displaySubViews:self indentLevel:0]; | |
} | |
- (void)displaySubViews:(UIView *)view indentLevel:(NSInteger)indentLevel | |
{ | |
NSMutableString *indentSpace = [NSMutableString new]; | |
for (NSInteger i = 0; i < indentLevel; i++) { | |
[indentSpace appendString:@"-"]; | |
} | |
NSLog(@"%@ %@", indentSpace, [view class]); | |
if (view.subviews.count > 0) { | |
for (UIView *subView in view.subviews) { | |
[self displaySubViews:subView indentLevel:indentLevel+1]; | |
} | |
} | |
} | |
- (UIView *)findSubView:(NSString *)className | |
{ | |
for (UIView *subView in self.subviews) { | |
if ([NSStringFromClass(subView.class) isEqualToString:className] == YES) { | |
return subView; | |
} | |
} | |
return nil; | |
} | |
- (NSArray *)findViewsInDescendent:(NSString *)className | |
{ | |
return [self findViewsInDescendent:className currentResult:[NSMutableArray new]]; | |
} | |
- (NSArray *)findViewsInDescendent:(NSString *)className currentResult:(NSMutableArray *)viewsFound | |
{ | |
if (viewsFound == nil) { | |
viewsFound = [NSMutableArray new]; | |
} | |
for (UIView *subView in self.subviews) { | |
if ([NSStringFromClass(subView.class) isEqualToString:className] == YES) { | |
[viewsFound addObject:subView]; | |
} | |
if(subView.subviews.count > 0) { | |
[subView findViewsInDescendent:className currentResult:viewsFound]; | |
} | |
} | |
return viewsFound; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment