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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// IBTTabBarController.m
// AceMTer
//
// Created by Xummer on 2/27/15.
// Copyright (c) 2015 Xummer. All rights reserved.
//
#import "IBTTabBarController.h"
#import "IBTBadgeView.h"
#import "UITabBarItem+Universal.h"
#import "IBTCommon.h"
@interface IBTTabBarController ()
{
NSMutableArray *m_arrBadgesViews;
NSMutableArray *m_arrTabBarBtns;
}
@end
@implementation IBTTabBarController
#pragma mark - Life Cycle
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
[self _init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self bringBadgeViewsToFront];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
m_arrBadgesViews = nil;
m_arrTabBarBtns = nil;
}
- (void)_init {
if ([self.tabBar respondsToSelector:@selector(setTranslucent:)]) {
self.tabBar.translucent = NO;
}
m_arrBadgesViews = [NSMutableArray array];
m_arrTabBarBtns = [NSMutableArray array];
}
#pragma mark - Public Method
- (void)setTabBarBadgeImage:(UIImage *)image forIndex:(NSUInteger)index {
UIViewController *vc = [self getTabBarBaseViewController:index];
if (!vc) {
return;
}
IBTBadgeView *badgeV = m_arrBadgesViews[ index ];
vc.tabBarItem.badgeValue = nil;
[badgeV setImage:image];
badgeV.hidden = image == nil;
}
- (void)setTabBarBadgeString:(NSString *)string forIndex:(NSUInteger)index {
UIViewController *vc = [self getTabBarBaseViewController:index];
if (!vc) {
return;
}
IBTBadgeView *badgeV = m_arrBadgesViews[ index ];
vc.tabBarItem.badgeValue = string;
badgeV.hidden = YES;
}
- (void)setTabBarBadgeValue:(NSInteger)value forIndex:(NSUInteger)index {
if (index >= [m_arrBadgesViews count]) {
return;
}
NSString *valueStr = [NSString stringWithFormat:@"%ld", (unsigned long)value];
[self setTabBarBadgeString:valueStr forIndex:index];
}
- (void)bringBadgeViewsToFront {
for (IBTBadgeView *badgeV in m_arrBadgesViews) {
[self.tabBar bringSubviewToFront:badgeV];
}
}
- (IBTBadgeView *)getBadgeViewForIndex:(NSUInteger)index {
if (index >= [m_arrBadgesViews count]) {
return nil;
}
return m_arrBadgesViews[ index ];
}
- (NSArray *)getBadgeViews {
return m_arrBadgesViews;
}
#pragma mark - Private Method
- (UIViewController *)getTabBarBaseViewController:(NSUInteger)index {
if (index >= [m_arrBadgesViews count]) {
return nil;
}
return self.viewControllers[ index ];
}
- (UIViewController *)currentViewController {
return self.viewControllers[ self.selectedIndex ];
}
- (void)setViewControllers:(NSArray *)controllers {
[m_arrBadgesViews removeAllObjects];
CGFloat perItemW = CGRectGetWidth(self.view.frame) / [controllers count];
NSInteger i = 0;
CGRect frame;
NSString *iconImgName = nil;
for (UIViewController *vCtrl in controllers) {
// Normal图片命名 Tab+|subname|+Icon+@2x.png
iconImgName = [NSString stringWithFormat:@"Tab%@", vCtrl.title];
UITabBarItem *tabBarItem =
[UITabBarItem itemWithTitle:[IBTCommon localizableString:vCtrl.title]
image:[UIImage imageNamed:iconImgName]
selectedImage:[UIImage imageNamed:[iconImgName stringByAppendingString:@"_HL"]]];
[tabBarItem setTitlePositionAdjustment:UIOffsetMake(0,-5)];
vCtrl.tabBarItem = tabBarItem;
// badge
frame = (CGRect){
.origin.x = perItemW * (i + 1) - 20 - 10,
.origin.y = 1,
.size.width = 20,
.size.height = 20
};
IBTBadgeView *badgeV = [[IBTBadgeView alloc] initWithFrame:frame];
badgeV.hidden = YES;
[m_arrBadgesViews addObject:badgeV];
[self.tabBar addSubview:badgeV];
i ++;
}
[super setViewControllers:controllers];
}
@end