UIView+FindUIViewController.m 2.17 KB
Newer Older
mei's avatar
mei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
//
//  UIView+FindUIViewController.m
//  JobTalk
//
//  Created by Xummer on 1/20/15.
//  Copyright (c) 2015 BST. All rights reserved.
//

#import "UIView+FindUIViewController.h"

@implementation UIView (FindUIViewController)

- (UIViewController *)firstAvailableUIViewController {
    // convenience function for casting and to "mask" the recursive function
    return (UIViewController *)[self traverseResponderChainForUIViewController];
}

- (id)traverseResponderChainForUIViewController {
    id nextResponder = [self nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        return nextResponder;
    }
    else if ([nextResponder isKindOfClass:[UIView class]]) {
        return [nextResponder traverseResponderChainForUIViewController];
    }
    else {
        return nil;
    }
}

@end

@implementation UIView (Extend)

- (NSArray *)subviewsWithClass:(Class)aClass {
    NSMutableArray *arrTmp = [NSMutableArray array];
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:aClass]) {
            [arrTmp addObject:view];
        }
    }
    
    return [arrTmp count] > 0 ? arrTmp : nil;
}

- (id)viewWithClass:(Class)aClass {
    __block id machedView = nil;
    
    [self.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:aClass]) {
            machedView = obj;
            *stop = YES;
        }
    }];
    
    return machedView;
}

- (void)removeSubViewWithClass:(Class)aClass {
    [[self viewWithClass:aClass] removeFromSuperview];
}

- (void)removeSubViewWithTag:(NSInteger)tag {
    [[self viewWithTag:tag] removeFromSuperview];
}

- (void)removeAllSubViews {
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}

- (void)autoresizingWithStrechFullSize {
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

- (void)autoresizingWithVerticalCenter {
    self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
}

- (void)autoresizingWithHorizontalCenter {
    self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
}

@end