单军华
2018-07-12 3e8437ae559487362fae3525beb79c534c213a51
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
//
//  CYLTabBarController.m
//  CYLTabBarController
//
//  v1.16.0 Created by 微博@iOS程序犭袁 ( http://weibo.com/luohanchenyilong/ ) on 10/20/15.
//  Copyright © 2015 https://github.com/ChenYilong . All rights reserved.
//
 
#import "UITabBarItem+CYLTabBarControllerExtention.h"
#import <objc/runtime.h>
#import "UIControl+CYLTabBarControllerExtention.h"
 
@implementation UITabBarItem (CYLTabBarControllerExtention)
 
+ (void)load {
    [self cyl_swizzleSetBadgeValue];
}
 
+ (void)cyl_swizzleSetBadgeValue {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cyl_ClassMethodSwizzle([self class], @selector(setBadgeValue:), @selector(cyl_setBadgeValue:));
    });
}
 
- (void)cyl_setBadgeValue:(NSString *)badgeValue {
    [self.cyl_tabButton cyl_removeTabBadgePoint];
    [self cyl_setBadgeValue:badgeValue];
}
 
- (UIControl *)cyl_tabButton {
    UIControl *control = [self valueForKey:@"view"];
    return control;
}
 
#pragma mark - private method
 
BOOL cyl_ClassMethodSwizzle(Class aClass, SEL originalSelector, SEL swizzleSelector) {
    Method originalMethod = class_getInstanceMethod(aClass, originalSelector);
    Method swizzleMethod = class_getInstanceMethod(aClass, swizzleSelector);
    BOOL didAddMethod =
    class_addMethod(aClass,
                    originalSelector,
                    method_getImplementation(swizzleMethod),
                    method_getTypeEncoding(swizzleMethod));
    if (didAddMethod) {
        class_replaceMethod(aClass,
                            swizzleSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzleMethod);
    }
    return YES;
}
 
@end