2016-08-31 114 views
3

我有標籤欄,我想從默認的灰色改變圖標的​​顏色爲白色, 我在AppDelegate中加入這一行改變標籤欄未選中的圖標顏色迅速

UITabBar.appearance().barTintColor = UIColor(red:0.51, green:0.39, blue:0.37, alpha:1.0) 

這是改變selected項目,如何我acn這樣做與非選擇?下面對標籤欄控制器,用於選擇和取消選擇的顏色變化標籤項 代碼根據

+0

如果要設置圖像標籤欄查看這些類似的問題這一次http://stackoverflow.com/a/38560183/6433023 –

+0

可能重複:http://stackoverflow.com /問題/ 30754026和http://stackoverflow.com/questions/29876722 –

回答

0

變化。

class TabbarController: UITabBarController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     UITabBar.appearance().tintColor = UIColor.purpleColor() 

     // set red as selected background color 
     let numberOfItems = CGFloat(tabBar.items!.count) 
     let tabBarItemSize = CGSize(width: tabBar.frame.width/numberOfItems, height: tabBar.frame.height) 
     tabBar.selectionIndicatorImage = UIImage.imageWithColor(UIColor.lightTextColor().colorWithAlphaComponent(0.5), size: tabBarItemSize).resizableImageWithCapInsets(UIEdgeInsetsZero) 

     // remove default border 
     tabBar.frame.size.width = self.view.frame.width + 4 
     tabBar.frame.origin.x = -2 

    } 

    override func viewWillAppear(animated: Bool) { 
     // For Images 
     let firstViewController:UIViewController = NotificationVC() 
     // The following statement is what you need 
     let customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "[email protected]")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "[email protected]")) 
     firstViewController.tabBarItem = customTabBarItem 

     for item in self.tabBar.items! { 
      let unselectedItem = [NSForegroundColorAttributeName: UIColor.whiteColor()] 
      let selectedItem = [NSForegroundColorAttributeName: UIColor.purpleColor()] 

      item.setTitleTextAttributes(unselectedItem, forState: .Normal) 
      item.setTitleTextAttributes(selectedItem, forState: .Selected) 
     } 
    } 
} 

extension UIImage { 
    class func imageWithColor(color: UIColor, size: CGSize) -> UIImage { 
     let rect: CGRect = CGRectMake(0, 0, size.width, size.height) 
     UIGraphicsBeginImageContextWithOptions(size, false, 0) 
     color.setFill() 
     UIRectFill(rect) 
     let image: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image 
    } 

} 
+0

你的意思是我要ctreate稱爲TabbarController新類? – joda

+0

是需要創建訪問標籤欄控制器的類,並在標籤欄內進行更改。 –

+0

我在這一行有錯誤'讓firstViewController:的UIViewController = NotificationVC()' – joda

1

假設你已經在你想要他們(在Photoshop或素描或其他)顏色的圖標,你可以通過屬性檢查器中做到這一點。

轉到您的故事板,找到你的視圖控制器(whcih應該已經嵌入到標籤欄控制器的話),並選擇在視圖控制器底部的標籤欄。

在屬性檢查器中,將條形圖項目下的「圖像」設置爲未選定的選項卡條形圖圖像(應該在您的資產中),並將選項卡條目下的「選定圖像」設置爲您選擇的版本。

接下來,進入你的資產目錄,選擇你的形象,並在屬性檢查器中,在映像集,設置渲染至於原始圖像。爲所有圖標做這個。

現在,這應該工作