2012-11-12 103 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 {} 
相關問題