2014-11-02 76 views
3

你好我想弄清楚如何使用我想要的颜色为我的tabBarswift UITabbaritem颜色

我知道如何改变背景,我也知道如何改变tabbar.item的颜色和文字。但我不明白如何:

  • 未选中的标签栏项目的默认灰色
  • 来改变颜色,如果该项目被选中(而我使用的渲染模式始终原来我的事业不能找到另一种方式来去除来自非选择的标签栏项目的默认灰色。)

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { 
    
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) 
    
        tabBarItem.title = "test" 
        tabBarItem.image = UIImage(named: "first.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) 
    
    } 
    

我该如何使用我想要的颜色,在我想要的状态?

回答

8

A UITabBar有一个tintColor属性,但它为所选图像设置色调,而不是未选定图像。您正确设置未选择的图像AFAIK。要更改所选图像的颜色,可以使用UITabBar上的tintColor(如果要让所有图像具有相同的色调),或将UITabBarItemselectedImage设置为AlwaysOriginal的渲染模式。

tabBarItem.selectedImage = UIImage(named: "first-selected")!.imageWithRenderingMode(.AlwaysOriginal) 

我将UIImage设置为unwrapped可选,因为如果没有图像文件,您可能希望它会崩溃。这将有助于确保您的图像实际上正在加载,而不是默默地失败:-)

您可能还想设置标签的颜色或您的文本将不匹配您的图像颜色。以下设置了所有UITabBarItem的默认设置,但您可以基于每个项目设置(或覆盖)它。

UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.blueColor()}, forState:.Selected) 
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.redColor()}, forState:.Normal) 
3

这里是你如何在迅速四分之三

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blue], for: .selected) 
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .normal) 
+1

你救我都做了。非常感谢 – 2017-07-28 18:13:10