2012-11-12 63 views

回答

4

看起来没有。您只能设置该值。 从苹果公司的文档徽章是:与一个 周围红色椭圆形显示于该项目的右上角

文本。

0

是的,但唯一可能的解决方案是创建一个自定义Tabbar并创建您的自定义标签栏徽章图标。你会发现很多文章/代码来创建自定义tabbar。

12

我写了这段代码我的应用程序,但我只在iOS的7测试它

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之后调用此代码!

编辑:14年4月14日

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

编辑:14年12月22日

下面是其中我目前使用更新的片段。为简单起见,我将代码放在了类别扩展中。

- (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) { 

}]; 

编辑:15年11月16日

它已经引起了我的注意,有些人需要对所发生的事情在这个代码更清晰。 for循环正在搜索几个不公开的视图。通过检查视图类名是否包含预期名称的一部分,它确保达到预期视图,同时不会引发Apple发出任何可能的红旗。一旦找到了所有东西,就可以轻松访问这些视图来执行一个块。

值得注意的是,该代码可能会在未来的iOS更新中停止工作。例如,这些内部视图有一天会获得不同的类名。然而,这种机会几乎没有,因为即使内部苹果也很少重构类。但即使他们这样做,它也会是UITabBarBadgeView的标题,它仍然会达到代码中的预期点。由于iOS9已经完成了,而且这些代码仍然按照预期工作,所以您可以预料到这个问题永远不会发生。

+0

提供的解决方案适用于iOS 7,但不适用于iOS7.1。任何事情都可以做到7.1? – CrazyDeveloper

+0

确保在您的视图控制器中调用了“-viewWillLayoutSubviews”。 – cnotethegr8

+7

@jeffamaphone OP问,'有可能'。他们从未要求实现不会中断。你给我弃权的理由是不够的。我请你好好扭转行动。 – cnotethegr8

10

我有同样的问题,并通过创建一个小的类别来代替BadgeView与你可以轻松定制的UILabel来解决它。

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

+1

这会被Apple拒绝吗? – Phillip

+1

不,我有一个使用此库的应用程序。 – eold

+0

您正在访问那里的私人财产。如果([STR isEqualToString:@“_ UIBadgeView”。?怎么会苹果不会拒绝它没有权限更改 –

7

不,你不能改变颜色,但你可以使用自己的徽章代替。在文件范围内添加此扩展名,然后您可以自定义标签。请在任何根视图控制器中拨打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 { 

} 
8

对于使用雨燕的人,我管理,将有工作的任何屏幕大小和任何方向徽章以提高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 {} 
5

改变徽章的颜色现在原生支持iOS中10及更高版本使用UITabbadgeColor财产。请参阅apple docs以了解酒店的更多信息。

实施例:

  • 夫特3:myTab.badgeColor = UIColor.blue
  • 目的-C:[myTab setBadgeColor:[UIColor blueColor]];
+1

如果([MYTAB respondsToSelector? :@选择(setBadgeColor :)]){ [MYTAB setBadgeColor:[的UIColor blueColor]];} 你 – rjobidon

+0

能解释一下这是如何实际执行?我有点困惑。 –

0
// 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]; 
        } 
       } 
      } 
     } 
    } 
} 

enter image description here

20

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

+0

当我还想支持iOS9时会发生什么?我是否需要将其放在'respondsToSelector'或'#available'调用中? – Jens

+1

不是那样的。 'if(@available(iOS 10,*))'... –

-2

看看这里@UITabbarItem-CustomBadge

的完整演示是继

Demonstration

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

- (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; 
} 
5

斯威夫特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 {} 
相关问题