Last active
December 22, 2015 08:59
-
-
Save spinogrizz/6449047 to your computer and use it in GitHub Desktop.
Two common used methods to find a subview of any given (private) class inside some view hierarchy, or just dump all views in a pretty-print format using NSLog.
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
// | |
// UIViewHierarchy.h | |
// | |
// Created by Denis Gryzlov on 09.08.13. | |
// Copyright (c) 2013 Armada. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIView (Hierarchy) | |
- (UIView *) findSubviewOfClass:(Class)someClass; | |
- (void) dumpViewHierarchy; | |
@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
// | |
// UIViewHierarchy.m | |
// | |
// Created by Denis Gryzlov on 09.08.13. | |
// Copyright (c) 2013 Armada. All rights reserved. | |
// | |
#import "UIViewHierarchy.h" | |
@implementation UIView (Hierarchy) | |
- (UIView *) findSubviewOfClass:(Class)someClass { | |
if ( [self isMemberOfClass:someClass] ) { | |
return self; | |
} else { | |
for ( UIView *view in self.subviews ) { | |
UIView *insideView = [view findSubviewOfClass:someClass]; | |
if ( insideView != nil ) { | |
return insideView; | |
} | |
} | |
} | |
return nil; | |
} | |
- (void) dumpViewHierarchy { | |
int level = 0; | |
[self _logViewHierarchyWithLevel:&level]; | |
} | |
- (void) _logViewHierarchyWithLevel:(int *)level { | |
NSMutableString *paddingStr = [NSMutableString new]; | |
for (int i = 0; i < *level; i++) { | |
[paddingStr appendString:@" "]; | |
} | |
NSLog(@"%@%@", paddingStr, self); | |
*level = *level + 1; | |
for ( UIView *subview in self.subviews ) { | |
if ( subview.subviews.count > 0 ) { | |
[subview _logViewHierarchyWithLevel:level]; | |
} | |
else { | |
NSLog(@"%@> %@", paddingStr, subview); | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output example: