添加/隐藏UITabBar右上角的小红点
1.添加tabBar的小红点
+ (void)tbShowBadgeOnItemIndex:(NSInteger)index withTabBar:(UITabBar *)tabbar withNumber:(NSInteger)num {
UIView *badgeView = [[UIView alloc] init];
badgeView.tag = 888 + index;
badgeView.backgroundColor = [UIColor redColor];
CGRect tabFrame = tabbar.frame;
float percentX = (index + 0.59) / 3;
CGFloat x = ceilf(percentX * tabFrame.size.width) - 4;
CGFloat y = ceilf(0.1 * tabFrame.size.height) - 2;
if (num <= 0) {
CGFloat badgeViewWidthAndHeight = 9.0;
badgeView.frame = CGRectMake(x, y, badgeViewWidthAndHeight, badgeViewWidthAndHeight);
} else {
CGFloat badgeViewWidthAndHeight = 15.0;
badgeView.frame = CGRectMake(x, y, badgeViewWidthAndHeight, badgeViewWidthAndHeight);
UILabel *numLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(badgeView.frame), CGRectGetHeight(badgeView.frame))];
numLabel.textAlignment = NSTextAlignmentCenter;
numLabel.text = [NSString stringWithFormat:@"%ld", num];
numLabel.font = [UIFont systemFontOfSize:10];
numLabel.textColor = [UIColor whiteColor];
[badgeView addSubview:numLabel];
}
badgeView.layer.cornerRadius = CGRectGetWidth(badgeView.frame)/2;
[tabbar addSubview:badgeView];
}
2.移除小红点
/** 移除小红点
* index:tabBar上的第几个索引
* tabbar:(self.tabBarController.tabBar)
*/
+ (void)tbRemoveBadgeOnItemIndex:(NSInteger)index withTabBar:(UITabBar *)tabbar {
for (UIView *subView in tabbar.subviews) {
if (subView.tag == 888 + index) {
[subView removeFromSuperview];
}
}
}