#import "UITabBar+badge.h"
#import "ContantHead.h"
#define TabbarBadge_Nums 5.0
@implementation UITabBar (badge)
//顯示小紅點(diǎn)
- (void)showBadgeOnItemIndex:(int)index withNum:(NSInteger)badgeNum{
//移除之前的小紅點(diǎn)
[self removeBadgeOnItemIndex:index];
//新建小紅點(diǎn)
UILabel *labNum = [[UILabel alloc]init];
labNum.tag = 888 + index;
labNum.layer.masksToBounds = YES;
labNum.layer.cornerRadius = 20/2;//圓形
labNum.backgroundColor = [UIColor redColor];//顏色:紅色
CGRect tabFrame = self.frame;
//確定小紅點(diǎn)的位置
float percentX = (index +0.6) / TabbarBadge_Nums;
CGFloat x = ceilf(percentX * tabFrame.size.width);
CGFloat y = ceilf(0.1 * tabFrame.size.height)-5;
labNum.frame = CGRectMake(x, y, 20, 20);//圓形大小為10
//-30*screenWidth/375
labNum.text = [NSString stringWithFormat:@"%ld",badgeNum];
labNum.textColor = [UIColor whiteColor];
labNum.adjustsFontSizeToFitWidth = YES;
labNum.textAlignment = NSTextAlignmentCenter;
[self addSubview:labNum];
}
//隱藏小紅點(diǎn)
- (void)hideBadgeOnItemIndex:(NSInteger)index{
//移除小紅點(diǎn)
[self removeBadgeOnItemIndex:index];
}
//移除小紅點(diǎn)
- (void)removeBadgeOnItemIndex:(NSInteger)index{
//按照tag值進(jìn)行移除
for (UIView *subView in self.subviews) {
if (subView.tag == 888+index) {
[subView removeFromSuperview];
}
}
}