2013-04-29 138 views

回答

1

嗯......改變內置徽章的背景似乎並不可能給我。但是可能的代替是:

子類TabBarController,構建一個自定義視圖放置在NIB中,將它添加到標籤欄中位置的視圖層次結構中,您希望它的位置。

在自定義視圖中,您可以將圖像設置爲背景,並在該背景上設置標籤,然後顯示數字值,然後可以通過代碼進行更改。

然後,您需要計算出自定義視圖的水平和垂直位置,以便將視圖放置在選項卡欄內。

希望這有助於一點點。 塞巴斯蒂安

+0

感謝您對這個想法。我會試試這個。但我有一個後續問題。這個徽章定製是否正確,如果這個應用程序將被提交給AppStore? – 2013-04-29 08:46:16

+0

歡迎您...如果您滿意,請熱情接受答案。 嗯....這是沒有問題的,因爲如果你不喜歡它,就不需要使用蘋果提供的徽章。如果您嘗試修改由Apple提供的私人課程,您只會遇到麻煩。 – sesc360 2013-04-29 09:21:23

8

這是你最好的選擇。在文件範圍內添加此擴展名,然後您可以自定義標籤。請在任何根視圖控制器中撥打self.tabBarController!.setBadges([1,0,2])

要明確這是一個帶有三個項目的標籤欄,徽章值從左到右。

如果要添加圖像,而不是僅僅改變addBadge方法

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 { 

} 
+0

這個App Store安全嗎? – 2015-09-29 16:05:07

+0

絕對的,標籤欄只是一個視圖,所以添加子視圖是完全合法的。 – TimWhiting 2015-10-02 22:04:58

1

您可以使用更強大的解決方案@UITabbarItem-CustomBadge

演示

enter image description here

簡單的兩行代碼可以讓你去

- (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; 
} 
+0

請留下評論的理由,如果有人會離開-1 – 2016-11-22 15:45:00

+0

通過我自己的github存儲庫的方式,我應該添加完整的6個代碼文件?你沒事吧? @Zoleas – 2016-11-22 16:49:06

相關問題