UITabBar+badge.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // UITabBar+badge.m
  3. // RCloudMessage
  4. //
  5. // Created by Jue on 16/7/1.
  6. // Copyright © 2016年 RongCloud. All rights reserved.
  7. //
  8. #import "UITabBar+badge.h"
  9. #define TabbarItemNums (ApplicationDelegate.tabbarVC.tabBarController.viewControllers.count)
  10. @implementation UITabBar (badge)
  11. - (void)bringSubviewToFrontIndex:(NSInteger)index {
  12. UIView *badge = [self viewWithTag:(888 + index)];
  13. if (badge) {
  14. [self bringSubviewToFront:badge];
  15. }
  16. }
  17. - (void)showBadgeOnItemIndex:(NSInteger)index {
  18. //移除之前的小红点
  19. [self removeBadgeOnItemIndex:index];
  20. UIImageView *badge = [[UIImageView alloc] init];
  21. badge.tag = 888 + index;
  22. badge.layer.cornerRadius = 4;
  23. badge.backgroundColor = [UIColor redColor];
  24. CGRect tabBarFrame = self.frame;
  25. //确定小红点的位置
  26. float percentX = (index + 0.6) / TabbarItemNums;
  27. CGFloat x = ceilf(percentX * tabBarFrame.size.width + 5);
  28. CGFloat y = ceilf(0.1 * tabBarFrame.size.height);
  29. badge.frame = CGRectMake(x, y, 8, 8);
  30. [self addSubview:badge];
  31. }
  32. - (void)showBadgeOnItemIndex:(NSInteger)index badgeValue:(NSInteger)badgeValue {
  33. //移除之前的小红点
  34. [self removeBadgeOnItemIndex:index];
  35. //新建小红点
  36. UIView *badgeView = [[UIView alloc]init];
  37. badgeView.tag = 888+index;
  38. badgeView.layer.cornerRadius = 9;//圆形
  39. badgeView.backgroundColor = [UIColor colorWithHexString:@"f43530" alpha:1.0];//颜色:红色
  40. CGRect tabFrame = self.frame;
  41. //新建Badge Label
  42. UILabel *badgeLabel = [[UILabel alloc] init];
  43. badgeLabel.font = [UIFont systemFontOfSize:12.f];
  44. badgeLabel.textColor = [UIColor whiteColor];
  45. badgeLabel.textAlignment = NSTextAlignmentCenter;
  46. [badgeView addSubview:badgeLabel];
  47. //确定小红点的位置
  48. float percentX = (index +0.55) / TabbarItemNums;
  49. CGFloat x = ceilf(percentX * tabFrame.size.width);
  50. CGFloat y = ceilf(0.1 * tabFrame.size.height);
  51. if (badgeValue < 10) {
  52. badgeView.frame = CGRectMake(x, y, 18, 18);//圆形
  53. badgeLabel.frame = CGRectMake(3, 3, 12, 12);
  54. badgeLabel.text = [NSString stringWithFormat:@"%ld",(long)badgeValue];
  55. }
  56. if (badgeValue >= 10 && badgeValue < 100 ) {
  57. badgeView.frame = CGRectMake(x, y, 22, 18);//椭圆形
  58. badgeLabel.frame = CGRectMake(1, 3, 20, 12);
  59. badgeLabel.text = [NSString stringWithFormat:@"%ld",(long)badgeValue];
  60. }
  61. if (badgeValue >= 100) {
  62. badgeView.frame = CGRectMake(x, y, 26, 18);//椭圆形
  63. badgeLabel.frame = CGRectMake(1, 0, 24, 10);
  64. badgeLabel.text = @"...";
  65. }
  66. [self addSubview:badgeView];
  67. }
  68. - (void)hideBadgeOnItemIndex:(NSInteger)index {
  69. //移除小红点
  70. [self removeBadgeOnItemIndex:index];
  71. }
  72. - (void)removeBadgeOnItemIndex:(NSInteger)index {
  73. UIView *badge = [self viewWithTag:(888 + index)];
  74. if (badge) {
  75. [badge removeFromSuperview];
  76. }
  77. }
  78. @end