是否有可能更改UITabBarItem徽章的颜色

我想更改UITabBarItem 徽章的背景颜色,但找不到任何资源如何使它。

在这里输入图像说明

UITabBarItem现在在iOS 10中可用

 var badgeColor: UIColor? { get set } 

这也可以通过出现。

 if #available(iOS 10, *) { UITabBarItem.appearance().badgeColor = .green } 

参考文档: https : //developer.apple.com/reference/uikit/uitabbaritem/1648567-badgecolor

我为我的应用程序编写了这段代码,但是我只在iOS 7中进行了testing。

 for (UIView* tabBarButton in self.tabBar.subviews) { for (UIView* badgeView in tabBarButton.subviews) { NSString* className = NSStringFromClass([badgeView class]); // looking for _UIBadgeView if ([className rangeOfString:@"BadgeView"].location != NSNotFound) { for (UIView* badgeSubview in badgeView.subviews) { NSString* className = NSStringFromClass([badgeSubview class]); // looking for _UIBadgeBackground if ([className rangeOfString:@"BadgeBackground"].location != NSNotFound) { @try { [badgeSubview setValue:[UIImage imageNamed:@"YourCustomImage.png"] forKey:@"image"]; } @catch (NSException *exception) {} } if ([badgeSubview isKindOfClass:[UILabel class]]) { ((UILabel *)badgeSubview).textColor = [UIColor greenColor]; } } } } } 

您只能用图像更新徽章背景,而不是颜色。 如果您想以某种方式更新徽章标签,我也会公开徽章标签。

重要的是要注意,这个代码必须在设置tabBarItem.badgeValue

编辑:4/14/14

上面的代码可以在任何地方调用iOS 7。 为了让它在iOS 7.1中工作,请在视图控制器-viewWillLayoutSubviews调用它。

编辑:12/22/14

这是我正在使用的更新片段。 为简单起见,我将代码放在了类别扩展中。

 - (void)badgeViews:(void (^)(UIView* badgeView, UILabel* badgeLabel, UIView* badgeBackground))block { if (block) { for (UIView* tabBarButton in self.subviews) { for (UIView* badgeView in tabBarButton.subviews) { NSString* className = NSStringFromClass([badgeView class]); if ([className rangeOfString:@"BadgeView"].location != NSNotFound) { UILabel* badgeLabel; UIView* badgeBackground; for (UIView* badgeSubview in badgeView.subviews) { NSString* className = NSStringFromClass([badgeSubview class]); if ([badgeSubview isKindOfClass:[UILabel class]]) { badgeLabel = (UILabel *)badgeSubview; } else if ([className rangeOfString:@"BadgeBackground"].location != NSNotFound) { badgeBackground = badgeSubview; } } block(badgeView, badgeLabel, badgeBackground); } } } } } 

那么当你准备好打电话的时候,它会看起来像这样。

 [self.tabBar badgeViews:^(UIView *badgeView, UILabel *badgeLabel, UIView *badgeBackground) { }]; 

编辑:11/16/15

有人提醒我,有些人需要更清楚一些这个代码中发生了什么。 for循环正在search几个不能公开访问的视图。 通过检查视图类名是否包含预期名称的一部分,它确保达到预期的视图,而不是由苹果发起任何可能的红旗。 一旦find了所有东西,就可以轻松地访问这些视图。

值得注意的是,这个代码可能会在未来的iOS更新中停止工作。 例如,这些内部视图可能有一天会获得不同的类名。 然而,这个机会几乎没有,因为即使是内部的苹果也很less重构类的本质。 但即使他们是这样,它将沿着UITabBarBadgeView的标题,这仍然会达到预期的代码点。 因为iOS9已经出来,而且这个代码仍然按照预期工作,所以你可以预料到这个问题永远不会出现。

我有同样的问题,并通过创build一个小类,用一个UILabelreplaceBadgeView来解决它,你可以轻松地自定义。

https://github.com/enryold/UITabBarItem-CustomBadge/

对于使用Swift的人,我设法改进了TimWhiting的答案,以便让徽章视图在任何屏幕尺寸和任何方向都能工作。

  extension UITabBarController { func setBadges(badgeValues: [Int]) { for view in self.tabBar.subviews { if view is CustomTabBadge { view.removeFromSuperview() } } for index in 0...badgeValues.count-1 { if badgeValues[index] != 0 { addBadge(index, value: badgeValues[index], color:UIColor(paletteItem: .Accent), font: UIFont(name: Constants.ThemeApp.regularFontName, size: 11)!) } } } func addBadge(index: Int, value: Int, color: UIColor, font: UIFont) { let badgeView = CustomTabBadge() badgeView.clipsToBounds = true badgeView.textColor = UIColor.whiteColor() badgeView.textAlignment = .Center badgeView.font = font badgeView.text = String(value) badgeView.backgroundColor = color badgeView.tag = index tabBar.addSubview(badgeView) self.positionBadges() } override public func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() self.positionBadges() } // Positioning func positionBadges() { var tabbarButtons = self.tabBar.subviews.filter { (view: UIView) -> Bool in return view.userInteractionEnabled // only UITabBarButton are userInteractionEnabled } tabbarButtons = tabbarButtons.sort({ $0.frame.origin.x < $1.frame.origin.x }) for view in self.tabBar.subviews { if view is CustomTabBadge { let badgeView = view as! CustomTabBadge self.positionBadge(badgeView, items:tabbarButtons, index: badgeView.tag) } } } func positionBadge(badgeView: UIView, items: [UIView], index: Int) { let itemView = items[index] let center = itemView.center let xOffset: CGFloat = 12 let yOffset: CGFloat = -14 badgeView.frame.size = CGSizeMake(17, 17) badgeView.center = CGPointMake(center.x + xOffset, center.y + yOffset) badgeView.layer.cornerRadius = badgeView.bounds.width/2 tabBar.bringSubviewToFront(badgeView) } } class CustomTabBadge: UILabel {} 

不,你不能改变颜色,但你可以使用自己的徽章。 在文件范围内添加此扩展名,您可以自定义标签。 只需在任何一个根视图控制器中调用self.tabBarController!.setBadges([1,0,2])

要明确这是为三个项目的标签栏,徽章值从左到右。

 extension UITabBarController { func setBadges(badgeValues:[Int]){ var labelExistsForIndex = [Bool]() for value in badgeValues { labelExistsForIndex.append(false) } for view in self.tabBar.subviews { if view.isKindOfClass(PGTabBadge) { let badgeView = view as! PGTabBadge let index = badgeView.tag if badgeValues[index]==0 { badgeView.removeFromSuperview() } labelExistsForIndex[index]=true badgeView.text = String(badgeValues[index]) } } for var i=0;i<labelExistsForIndex.count;i++ { if labelExistsForIndex[i] == false { if badgeValues[i] > 0 { addBadge(i, value: badgeValues[i], color:UIColor(red: 4/255, green: 110/255, blue: 188/255, alpha: 1), font: UIFont(name: "Helvetica-Light", size: 11)!) } } } } func addBadge(index:Int,value:Int, color:UIColor, font:UIFont){ let itemPosition = CGFloat(index+1) let itemWidth:CGFloat = tabBar.frame.width / CGFloat(tabBar.items!.count) let bgColor = color let xOffset:CGFloat = 12 let yOffset:CGFloat = -9 var badgeView = PGTabBadge() badgeView.frame.size=CGSizeMake(17, 17) badgeView.center=CGPointMake((itemWidth * itemPosition)-(itemWidth/2)+xOffset, 20+yOffset) badgeView.layer.cornerRadius=badgeView.bounds.width/2 badgeView.clipsToBounds=true badgeView.textColor=UIColor.whiteColor() badgeView.textAlignment = .Center badgeView.font = font badgeView.text = String(value) badgeView.backgroundColor = bgColor badgeView.tag=index tabBar.addSubview(badgeView) } } class PGTabBadge: UILabel { } 

现在在iOS 10中badgeColor支持更改徽章颜色,并使用badgeColor属性。 查看苹果文档了解更多信息。

例:

  • Swift 3: myTab.badgeColor = UIColor.blue
  • Objective-C: [myTab setBadgeColor:[UIColor blueColor]];

看来没有。 您只能设置值。 从苹果的文档徽章是:

显示在项目右上angular的文字周围的红色椭圆形。

Swift 3这里是@ Kirualex的回答(谁在@ TimWhiting的回答中得到了改进)的更新版本Swift 3。

 extension UITabBarController { func setBadges(badgeValues: [Int]) { for view in self.tabBar.subviews { if view is CustomTabBadge { view.removeFromSuperview() } } for index in 0...badgeValues.count-1 { if badgeValues[index] != 0 { addBadge(index: index, value: badgeValues[index], color: UIColor.blue, font: UIFont(name: "Helvetica-Light", size: 11)!) } } } func addBadge(index: Int, value: Int, color: UIColor, font: UIFont) { let badgeView = CustomTabBadge() badgeView.clipsToBounds = true badgeView.textColor = UIColor.white badgeView.textAlignment = .center badgeView.font = font badgeView.text = String(value) badgeView.backgroundColor = color badgeView.tag = index tabBar.addSubview(badgeView) self.positionBadges() } override open func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() self.positionBadges() } // Positioning func positionBadges() { var tabbarButtons = self.tabBar.subviews.filter { (view: UIView) -> Bool in return view.isUserInteractionEnabled // only UITabBarButton are userInteractionEnabled } tabbarButtons = tabbarButtons.sorted(by: { $0.frame.origin.x < $1.frame.origin.x }) for view in self.tabBar.subviews { if view is CustomTabBadge { let badgeView = view as! CustomTabBadge self.positionBadge(badgeView: badgeView, items:tabbarButtons, index: badgeView.tag) } } } func positionBadge(badgeView: UIView, items: [UIView], index: Int) { let itemView = items[index] let center = itemView.center let xOffset: CGFloat = 12 let yOffset: CGFloat = -14 badgeView.frame.size = CGSize(width: 17, height: 17) badgeView.center = CGPoint(x: center.x + xOffset, y: center.y + yOffset) badgeView.layer.cornerRadius = badgeView.bounds.width/2 tabBar.bringSubview(toFront: badgeView) } } class CustomTabBadge: UILabel {} 

是的,但唯一可行的解​​决scheme是创build一个自定义的Tabbar和创build自定义的标签栏徽章图标。 你会发现很多文章/代码来创build自定义的标签栏。

 // change TabBar BadgeView background Color -(void)changeTabBarBadgeViewBgColor:(UITabBar*)tabBar { for (UIView* tabBarButton in tabBar.subviews) { for (UIView* badgeView in tabBarButton.subviews) { NSString* className = NSStringFromClass([badgeView class]); // looking for _UIBadgeView if ([className rangeOfString:@"BadgeView"].location != NSNotFound) { for (UIView* badgeSubview in badgeView.subviews) { NSString* className = NSStringFromClass([badgeSubview class]); // looking for _UIBadgeBackground if ([className rangeOfString:@"BadgeBackground"].location != NSNotFound) { @try { [badgeSubview setValue:nil forKey:@"image"]; [badgeSubview setBackgroundColor:[UIColor blueColor]]; badgeSubview.clipsToBounds = YES; badgeSubview.layer.cornerRadius = badgeSubview.frame.size.height/2; } @catch (NSException *exception) {} } if ([badgeSubview isKindOfClass:[UILabel class]]) { ((UILabel *)badgeSubview).textColor = [UIColor greenColor]; } } } } } } 

在这里输入图像说明

看看@ UITabbarItem-CustomBadge 。

以下是一个完整的演示

示范

它只需要两行代码,如果你想使用默认的实现

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //supplying the animation parameter [UITabBarItem setDefaultAnimationProvider:[[DefaultTabbarBadgeAnimation alloc] init]]; [UITabBarItem setDefaultConfigurationProvider:[[DefaultSystemLikeBadgeConfiguration alloc] init]]; //rest of your code goes following... return YES; }