您的位置:首页 > 产品设计 > UI/UE

iOS如何自定义的设置UITabbarItem的badge

2016-10-19 14:33 686 查看
本文的代码实现效果如下:



除了第三个tabbarItem显示数字,其他的都显示红点



代码如下:

//tabbar消息未读数量
- (void)showUnreadCountViewItemNO:(NSInteger)index unReadCountSum:(NSInteger)unReadCountSum {
UILabel *bageView = (UILabel*)[self.tabBarController.tabBar subviewWithTag:1000+index];
if(unReadCountSum){
if(!bageView){
bageView = [[UILabel alloc] init];
bageView.backgroundColor = kDefaultColor;
bageView.tag = 1000+index;
[self.tabBarController.tabBar addSubview:bageView];
if(index == 2){
bageView.frame=CGRectMake(WIDTH/4/2+WIDTH/4*index+8, 4, 16, 16);
[CALayer updateControlLayer:bageView.layer radius:8 borderWidth:0 borderColor:nil];
bageView.textAlignment = NSTextAlignmentCenter;
bageView.textColor = WHITE_COLOR;
}else{
bageView.frame=CGRectMake(WIDTH/4/2+WIDTH/4*index+8, 4, 10, 10);
[CALayer updateControlLayer:bageView.layer radius:5 borderWidth:0 borderColor:nil];
}
}
if(index == 2){
NSString *numStr;
if(unReadCountSum>100){
numStr = @"99+";
bageView.font = FONT_SYSTEM_SIZE(10);
}else{
numStr = [NSString stringWithFormat:@"%ld", (long)unReadCountSum];
bageView.font = FONT_SYSTEM_SIZE(12);
}
bageView.text = numStr;
[UIApplication sharedApplication].applicationIconBadgeNumber = unReadCountSum;
}
}else{
if(bageView){
[bageView removeFromSuperview];
}
if(index==2){
[UIApplication sharedApplication].applicationIconBadgeNumber = unReadCountSum;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UItabbarItem badge