iOS如何自定義的設定UITabbarItem的badge
阿新 • • 發佈:2019-01-07
本文的程式碼實現效果如下:
除了第三個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; } } }