自定义tabbar

本文介绍了一个自定义的iOS Tab栏通知视图实现方案,包括如何显示通知计数和更新图标状态。通过创建MyNBTabNotification和MyNBTabButton类,可以方便地为Tab栏按钮添加通知提示。

#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

 

附上完整代码https://github.com/Andrew5/TabbarController

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值