系統方法:
系統自帶的方法可以顯示具體的消息數量,這個就是蘋果設備常見的小紅點。實現思路如下:
NSArray *tabBarItems = self.navigationController.tabBarController.tabBar.items;
UITabBarItem *personCenterTabBarItem = [tabBarItems objectAtIndex:3];
personCenterTabBarItem.badgeValue = @"2";//顯示消息條數為 2
自定義方法:
自己將小紅點圖標放在想要顯示的位置,控制UIImageView的hidden屬性即可。實現思路如下:
UIImageView *dotImage = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"red_point_small"]];
dotImage.backgroundColor = [UIColorclearColor];
CGRect tabFrame =self.navigationController.tabBarController.tabBar.frame;
CGFloat x =ceilf(0.9 * tabFrame.size.width);
CGFloat y =ceilf(0.1 * tabFrame.size.height);
dotImage.frame =CGRectMake(x, y, 8,8);
[self.navigationController.tabBarController.tabBaraddSubview:dotImage];
App的桌面應用圖標上的消息提示,實現思路如下:
if ([[XWGlobalHelper systemVersion] intValue] > 7.99 && [[XWGlobalHelper systemVersion] intValue] < 9.001) {
//IOS8 需要 設置
UIUserNotificationSettings *settings = [UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
[UIApplication sharedApplication].applicationIconBadgeNumber = 3;
