//
//  UITabBar+badge.m
//  Huangshan
//
//  Created by xiejiangbo on 2018/12/29.
//  Copyright © 2018 yin chen. All rights reserved.
//

#import "UITabBar+badge.h"
#define TabbarItemNums 5.0    //tabbar的数量
@implementation UITabBar (badge)
- (void)showBadgeOnItemIndex:(int)index angNum:(NSString *)num{
    //移除之前的小红点
    [self removeBadgeOnItemIndex:index];
    if ([num isEqualToString:@"0"] || [num isEqualToString:@""]) {
        return;
    }
    
    
    //新建小红点
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = 888 + index;
//    btn.layer.cornerRadius = 6;
//    btn.backgroundColor = [UIColor redColor];
    [btn setBackgroundImage:[UIImage imageNamed:@"biaoji_image"] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:10.0];
    btn.titleLabel.textColor = [UIColor whiteColor];
    [btn setTitle:num forState:UIControlStateNormal];
    CGRect tabFrame = self.frame;
    
    //确定小红点的位置
    float percentX = (index +0.6) / TabbarItemNums;
    CGFloat x = ceilf(percentX * tabFrame.size.width);
    CGFloat y = ceilf(0.1 * tabFrame.size.height);
    btn.frame = CGRectMake(x, y, 21, 21);
    [self addSubview:btn];
    
}

- (void)hideBadgeOnItemIndex:(int)index{
    
    //移除小红点
    [self removeBadgeOnItemIndex:index];
    
}

- (void)removeBadgeOnItemIndex:(int)index{
    
    //按照tag值进行移除
    for (UIView *subView in self.subviews) {
        
        if (subView.tag == 888+index) {
            
            [subView removeFromSuperview];
            
        }
    }
}

@end
