#define FONT_SIZE 12
@implementation MyNBTabNotification
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
notificationCount = 0;
imageView = [[UIImageView alloc] init];
imageView.frame = self.frame;
[self addSubview:imageView];
countLabel = [[UILabel alloc] init];
countLabel.backgroundColor = [UIColor clearColor];
countLabel.textColor = [UIColor whiteColor];
countLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
countLabel.textAlignment = NSTextAlignmentCenter;
countLabel.frame = self.frame;
[self addSubview:countLabel];
self.userInteractionEnabled = NO;
}
return self;
}
- (void)setAllFrames:(CGRect)frame{
self.frame = frame;
imageView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
countLabel.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
}
- (NSInteger)notificationCount {
return notificationCount;
}
- (void)addNotifications:(NSInteger)n{
if (n > 0) {
notificationCount += n;
}
[self updateImageView];
}
- (void)removeNotifications:(NSInteger)n{
if (n > 0) {
notificationCount -= n;
}
if (notificationCount < 0) notificationCount = 0;
[self updateImageView];
}
- (void)updateImageView{
NSString *notificationLightIconFile;
if ([self notificationCount] <= 0) {
notificationLightIconFile = @"";
}else{
notificationLightIconFile = @"tabBarNotificationLightOn";
}
UIImage *notificationLight = [UIImage imageNamed:notificationLightIconFile];
imageView.image = notificationLight;
if ([self notificationCount] > 0 && [self notificationCount] < 100) {
countLabel.text = [NSString stringWithFormat:@"%ld",(long)[self notificationCount]];
}else if ([self notificationCount] >= 100){
countLabel.text = @"99+";
}else{
countLabel.text = @"";
}
}
@end
@interface MyNBTabNotification : UIView{
UIImageView *imageView;
UILabel *countLabel;
NSInteger notificationCount;
}
-(NSInteger)notificationCount;
-(void)addNotifications:(NSInteger)n;
-(void)removeNotifications:(NSInteger)n;
-(void)setAllFrames:(CGRect)frame;
-(void)updateImageView;
@end
@interface MyNBTabButton : NSObject {
UIImage *icon;
UIImage *highlightedIcon;
NSMutableArray *_notifications;
MyNBTabNotification *_notification;
}
@property (nonatomic,strong) UIImage *icon;
@property (nonatomic,strong) UIImage *highlightedIcon;
@property (nonatomic,strong) MyNBTabController *viewController;
- (id)initWithIcon:(UIImage *)icon;
- (void)addNotification:(NSDictionary *)notification;
- (void)removeNotification:(NSUInteger)index;
- (void)clearNotifications;
- (NSInteger)notificationCount;
- (MyNBTabNotification *)notificationView;
@end
#import "MyNBTabButton.h"
@interface MyNBTabButton()
@property (nonatomic,strong) NSMutableArray *notifications;
@property (nonatomic,strong) MyNBTabNotification *light;
@end
@implementation MyNBTabButton
@synthesize notifications = _notifications, viewController = _viewController, light = _light, icon, highlightedIcon;
- (id)initWithIcon:(UIImage *)nIcon {
self = [super init];
if (self) {
self.icon = nIcon;
self.light = [[MyNBTabNotification alloc] initWithFrame:CGRectMake(0, 0, 24, 27)];
self.notifications = [[NSMutableArray alloc]init];
}
return self;
}
- (void)setViewController:(MyNBTabController *)viewController {
_viewController = viewController;
self.viewController.tabBarButton = self;
}
- (MyNBTabNotification *)notificationView {
return self.light;
}
- (void)addNotification:(NSDictionary *)notification {
[self.notifications insertObject:notification atIndex:0];
[self.light addNotifications:1];
}
- (void)removeNotification:(NSUInteger)index {
if ([self.notifications count] > 0) {
[self.notifications removeObjectAtIndex:index];
[self.light removeNotifications:1];
}
}
- (void)clearNotifications {
[self.light removeNotifications:[self.notifications count]];
[self.notifications removeAllObjects];
}
- (NSInteger)notificationCount {
return [self.notifications count];
}
@end