一.系统自带
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"首页";
UITabBarItem *item = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1];
self.tabBarItem = item;
// NSArray *tabBarItems = self.navigationController.tabBarController.tabBar.items;
// // 获取到需要展示消息的tabbarItem,属性badgeValue(标记个数)就是消息的条数
// UITabBarItem *personCenterTabBarItem = [tabBarItems objectAtIndex:3];
// personCenterTabBarItem.badgeValue = @"2";//显示消息条数为 2
item.badgeValue = @"99+";
二.自定义
// 自定义方法:
// 自己将小红点图标放在想要显示的位置,控制UIImageView的hidden属性即可。实现思路如下:
// 创建一个UIImageView,存放小红点图片
UIImageView *dotImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"red_point_small"]];
dotImage.backgroundColor = [UIColor redColor];
// 获取tabbarItame的frame
CGRect tabFrame =self.navigationController.tabBarController.tabBar.frame;
CGFloat x =ceilf(0.5 * tabFrame.size.width);
CGFloat y =ceilf(0.1 * tabFrame.size.height);
// 设置小红点图标的frame
dotImage.frame =CGRectMake(x, y, 8,8);
// 将小红点imageView添加到tabbar上面
[self.navigationController.tabBarController.tabBar addSubview:dotImage];
三.桌面icon图标红点
//获取用户授权
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
//注册消息推送
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
//设置推送消息个数
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:10];