2014-04-13 44 views
4

更改UITabBarItem徽章的顏色,我不能修改UItabBarItem的徽章顏色的iOS 7.1,所以我只是增加了一個UILabel到的TabBar,並設置我想要的顏色,並得到了在IOS 7.1的工作。但我不確定這是否是正確的做法。我將提交我的應用程序到appstore。如果有可能我的應用因此被拒絕,有人可以指導我嗎?以下是我的代碼。在IOS 7.1

UILabel *badge=[[UILabel alloc]init]; 
badge.text = @"2"; 
badge.textAlignment=NSTextAlignmentCenter; 
badge.frame=CGRectMake(122, 1, 20, 20); 
badge.layer.cornerRadius=10; 
badge.textColor=[UIColor whiteColor]; 
badge.backgroundColor=[UIColor greenColor]; 
[tabbar addSubview:badge]; 
+1

你沒有使用任何私人API或違反任何指導方針,所以你的應用程序將沒事。 –

回答

3

您不能修改徽章的顏色,因爲它沒有暴露給用戶。你只能將文本設置爲字符串

@property(nonatomic,copy) NSString *badgeValue; // default is nil 

你在做什麼都很好。

0

不,你不能改變顏色,但你正在做使用自己的徽章,而不是正確的事情。在文件範圍內添加此擴展名,然後您可以自定義標籤。請在任何根視圖控制器中撥打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 { 

}