某项目 需要在UITabbar 上显示小红点,在此搜罗了三个方法。

1.使用系统自带的,并且可以在小红点上显示数字。

 [itemOne setBadgeValue:@""]; //显示不带数字的小红点
 [itemOne setBadgeValue:@"1"];//显示小红点 并且带数字

以上的缺点:小红点太大了,伤不起啊!

2.使用图片,创建图片的时候,设置图片的渲染。

 UIImage * normalImage = [[UIImage imageNamed:@"pic_msg_yes_nor"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            UIImage * selectImage = [[UIImage imageNamed:@"pic_msg_yes_sel"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            UITabBarItem *itemOne=[[UITabBarItem alloc]initWithTitle:@"消息" image:normalImage selectedImage:selectImage];

/**设置UIImage的渲染模式:UIImage.renderingMode

着色(Tint Color)是iOS7界面中的一个.设置UIImage的渲染模式:UIImage.renderingMode重大改变,你可以设置一个UIImage在渲染时是否使用当前视图的Tint Color。UIImage新增了一个只读属性:renderingMode,对应的还有一个新增方法:imageWithRenderingMode:,它使用UIImageRenderingMode枚举值来设置图片的renderingMode属性。该枚举中包含下列值:

UIImageRenderingModeAutomatic  // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。  
UIImageRenderingModeAlwaysOriginal   // 始终绘制图片原始状态,不使用Tint Color。  
UIImageRenderingModeAlwaysTemplate   // 始终根据Tint Color绘制图片,忽略图片的颜色信息。  
 
renderingMode属性的默认值是UIImageRenderingModeAutomatic
*/

缺点:这样就只能显示小红点了,无法显示具体的数字。不过楼主的项目 不需要显示数字,就用了这种。方便简洁。

3.使用catgory,扩展UITabbar

此方法转载自:http://blog.csdn.net/lilinoscar/article/details/47103747

第一步,建一个UITabBar的category类别。

这里写图片描述

第二步,编写代码。

.h文件

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface UITabBar (badge)  
  4.   
  5. - (void)showBadgeOnItemIndex:(int)index;   //显示小红点  
  6.   
  7. - (void)hideBadgeOnItemIndex:(int)index; //隐藏小红点  
  8.   
  9. @end  


.m文件

  1. #import "UITabBar+badge.h"  
  2. #define TabbarItemNums 4.0    //tabbar的数量 如果是5个设置为5.0  
  3.   
  4. @implementation UITabBar (badge)  
  1. //显示小红点  
  2. - (void)showBadgeOnItemIndex:(int)index{    
  3.     //移除之前的小红点  
  4.     [self removeBadgeOnItemIndex:index];  
  5.   
  6.     //新建小红点  
  7.     UIView *badgeView = [[UIView alloc]init];  
  8.     badgeView.tag = 888 + index;  
  9.     badgeView.layer.cornerRadius = 5;//圆形  
  10.     badgeView.backgroundColor = [UIColor redColor];//颜色:红色  
  11.     CGRect tabFrame = self.frame;  
  12.   
  13.     //确定小红点的位置  
  14.     float percentX = (index +0.6) / TabbarItemNums;  
  15.     CGFloat x = ceilf(percentX * tabFrame.size.width);  
  16.     CGFloat y = ceilf(0.1 * tabFrame.size.height);  
  17.     badgeView.frame = CGRectMake(x, y, 10, 10);//圆形大小为10  
  18.     [self addSubview:badgeView];  
  19. }  
  1. //隐藏小红点  
  2. - (void)hideBadgeOnItemIndex:(int)index{  
  3.     //移除小红点  
  4.     [self removeBadgeOnItemIndex:index];  
  5. }  
  1. //移除小红点  
  2. - (void)removeBadgeOnItemIndex:(int)index{  
  3.     //按照tag值进行移除  
  4.     for (UIView *subView in self.subviews) {  
  5.         if (subView.tag == 888+index) {  
  6.             [subView removeFromSuperview];  
  7.         }  
  8.     }  
  9. }  
  10.   
  11. @end  

第三步,引入到需要使用的类中。

  1. #import "UITabBar+badge.h"  

引用代码如下:

    1. //显示  
    2. [self.tabBarController.tabBar showBadgeOnItemIndex:2];  
    3. //隐藏  
    4. [self.tabBarController.tabBar hideBadgeOnItemIndex:2]  
原文地址:https://www.cnblogs.com/niit-soft-518/p/4772515.html