猎豆优选

UITabBar+LDExtension.m 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // UITabBar+LDExtension.m
  3. // YouHuiProject
  4. //
  5. // Created by liuxueli on 2019/1/15.
  6. // Copyright © 2019 kuxuan. All rights reserved.
  7. //
  8. #import "UITabBar+LDExtension.h"
  9. #define TabbarItemNums 5.0 //tabbar的数量 如果是5个设置为5
  10. @implementation UITabBar (LDExtension)
  11. //显示小红点
  12. - (void)showBadgeOnItemIndex:(NSInteger)index{
  13. //移除之前的小红点
  14. [self removeBadgeOnItemIndex:index];
  15. //新建小红点
  16. UIView *badgeView = [[UIView alloc]init];
  17. badgeView.tag = 888 + index;
  18. badgeView.layer.cornerRadius = 5.0;//圆形
  19. badgeView.backgroundColor = [UIColor redColor];//颜色:红色
  20. CGRect tabFrame = self.frame;
  21. //确定小红点的位置
  22. CGFloat percentX = (index + 0.6) / TabbarItemNums;
  23. CGFloat x = ceilf(percentX * tabFrame.size.width);
  24. CGFloat y = ceilf(0.1 * tabFrame.size.height);
  25. badgeView.frame = CGRectMake(x, y, 10.0, 10.0);//圆形大小为10
  26. badgeView.clipsToBounds = YES;
  27. [self addSubview:badgeView];
  28. }
  29. //隐藏小红点
  30. - (void)hideBadgeOnItemIndex:(NSInteger)index{
  31. //移除小红点
  32. [self removeBadgeOnItemIndex:index];
  33. }
  34. //移除小红点
  35. - (void)removeBadgeOnItemIndex:(NSInteger)index{
  36. //按照tag值进行移除
  37. for (UIView *subView in self.subviews) {
  38. if (subView.tag == 888+index) {
  39. [subView removeFromSuperview];
  40. }
  41. }
  42. }
  43. @end