iOS tabbar 角标
#import "UITabBar+badge.h"
#import "ContantHead.h"
#define TabbarBadge_Nums 5.0
@implementation UITabBar (badge)
//显示小红点
- (void)showBadgeOnItemIndex:(int)index withNum:(NSInteger)badgeNum{
//移除之前的小红点
[self removeBadgeOnItemIndex:index];
//新建小红点
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;
//确定小红点的位置
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];
}
//隐藏小红点
- (void)hideBadgeOnItemIndex:(NSInteger)index{
//移除小红点
[self removeBadgeOnItemIndex:index];
}
//移除小红点
- (void)removeBadgeOnItemIndex:(NSInteger)index{
//按照tag值进行移除
for (UIView *subView in self.subviews) {
if (subView.tag == 888+index) {
[subView removeFromSuperview];
}
}
}