1.系统自带方法
[item setBadgeValue:@" "];//显示不带数字的小红点," "中间为空格 这样设置出来的小红点太大了,超级不好看
2.使用catgory,扩展UITabbar
.h文件
#import
@interfaceUITabBar (badge)
- (void)showBadgeOnItemIndex:(int)index; //显示小红点
- (void)hideBadgeOnItemIndex:(int)index;//隐藏小红点
@end
.m文件
#import "UITabBar+badge.h"
#define TabbarItemNums4.0//tabbar的数量,根据情况设置
@implementationUITabBar (badge)
//显示小红点
- (void)showBadgeOnItemIndex:(int)index{
//移除之前的小红点
[self removeBadgeOnItemIndex:index];
//新建小红点
UIView*badgeView = [[UIViewalloc]init];
badgeView.tag=888+ index;
badgeView.layer.cornerRadius=5;//圆形
badgeView.backgroundColor = [UIColor redColor];//颜色:红色
CGRecttabFrame =self.frame;
//确定小红点的位置
floatpercentX = (index +0.6) /TabbarItemNums;
CGFloatx =ceilf(percentX * tabFrame.size.width);
CGFloaty =ceilf(0.1* tabFrame.size.height);
badgeView.frame=CGRectMake(x, y,10,10);//圆形大小为10
[selfaddSubview:badgeView];
}
//隐藏小红点
- (void)hideBadgeOnItemIndex:(int)index{
//移除小红点
[self removeBadgeOnItemIndex:index];
}
//移除小红点
- (void)removeBadgeOnItemIndex:(int)index{
//按照tag值进行移除
for(UIView*subViewinself.subviews) {
if(subView.tag==888+index) {
[subViewremoveFromSuperview];
}
}
}
@end
使用: (1).导入头文件
(2). //显示
[self.tabBarController.tabBarshowBadgeOnItemIndex:1];
//隐藏
[self.tabBarController.tabBarhideBadgeOnItemIndex:1];