12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- //
- // UITabBar+badge.m
- // RCloudMessage
- //
- // Created by Jue on 16/7/1.
- // Copyright © 2016年 RongCloud. All rights reserved.
- //
- #import "UITabBar+badge.h"
- #define TabbarItemNums (ApplicationDelegate.tabbarVC.tabBarController.viewControllers.count)
- @implementation UITabBar (badge)
- - (void)bringSubviewToFrontIndex:(NSInteger)index {
- UIView *badge = [self viewWithTag:(888 + index)];
- if (badge) {
- [self bringSubviewToFront:badge];
- }
- }
- - (void)showBadgeOnItemIndex:(NSInteger)index {
-
- //移除之前的小红点
- [self removeBadgeOnItemIndex:index];
-
- UIImageView *badge = [[UIImageView alloc] init];
- badge.tag = 888 + index;
- badge.layer.cornerRadius = 4;
- badge.backgroundColor = [UIColor redColor];
- CGRect tabBarFrame = self.frame;
-
- //确定小红点的位置
- float percentX = (index + 0.6) / TabbarItemNums;
- CGFloat x = ceilf(percentX * tabBarFrame.size.width + 5);
- CGFloat y = ceilf(0.1 * tabBarFrame.size.height);
- badge.frame = CGRectMake(x, y, 8, 8);
- [self addSubview:badge];
- }
- - (void)showBadgeOnItemIndex:(NSInteger)index badgeValue:(NSInteger)badgeValue {
-
- //移除之前的小红点
- [self removeBadgeOnItemIndex:index];
-
- //新建小红点
- UIView *badgeView = [[UIView alloc]init];
- badgeView.tag = 888+index;
- badgeView.layer.cornerRadius = 9;//圆形
- badgeView.backgroundColor = [UIColor colorWithHexString:@"f43530" alpha:1.0];//颜色:红色
- CGRect tabFrame = self.frame;
-
- //新建Badge Label
- UILabel *badgeLabel = [[UILabel alloc] init];
- badgeLabel.font = [UIFont systemFontOfSize:12.f];
- badgeLabel.textColor = [UIColor whiteColor];
- badgeLabel.textAlignment = NSTextAlignmentCenter;
- [badgeView addSubview:badgeLabel];
-
- //确定小红点的位置
- float percentX = (index +0.55) / TabbarItemNums;
- CGFloat x = ceilf(percentX * tabFrame.size.width);
- CGFloat y = ceilf(0.1 * tabFrame.size.height);
- if (badgeValue < 10) {
- badgeView.frame = CGRectMake(x, y, 18, 18);//圆形
- badgeLabel.frame = CGRectMake(3, 3, 12, 12);
- badgeLabel.text = [NSString stringWithFormat:@"%ld",(long)badgeValue];
- }
- if (badgeValue >= 10 && badgeValue < 100 ) {
- badgeView.frame = CGRectMake(x, y, 22, 18);//椭圆形
- badgeLabel.frame = CGRectMake(1, 3, 20, 12);
- badgeLabel.text = [NSString stringWithFormat:@"%ld",(long)badgeValue];
- }
- if (badgeValue >= 100) {
- badgeView.frame = CGRectMake(x, y, 26, 18);//椭圆形
- badgeLabel.frame = CGRectMake(1, 0, 24, 10);
- badgeLabel.text = @"...";
- }
-
- [self addSubview:badgeView];
-
- }
- - (void)hideBadgeOnItemIndex:(NSInteger)index {
-
- //移除小红点
- [self removeBadgeOnItemIndex:index];
- }
- - (void)removeBadgeOnItemIndex:(NSInteger)index {
-
- UIView *badge = [self viewWithTag:(888 + index)];
- if (badge) {
- [badge removeFromSuperview];
- }
- }
- @end
|