简体   繁体   English

更改图标颜色选项卡栏控制器-SWIFT

[英]Change Icon Color Tab Bar Controller-SWIFT

I am working in Xcode 6 with SWIFT and have been able to successfully change the color of the text on my tab bar controller, but I cannot get the color to change on the icons themselves. 我正在使用SWIFT在Xcode 6中进行操作,并且能够成功更改标签栏控制器上文本的颜色,但是我无法在图标本身上更改颜色。 My code below in the AppDelegate: 我在AppDelegate中的以下代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red: 239/255, green: 64/255, blue: 54/255, alpha: 1.0)], forState: .Selected)
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: .Normal)

I have a separate class for the navigation controller to change text and color. 我有一个单独的类,用于导航控制器来更改文本和颜色。

You need to set the image on the tab bar item to a UIImage with renderingMode = UIImageRenderingMode.AlwaysTemplate 您需要将选项卡栏项上的image设置为UIImagerenderingMode = UIImageRenderingMode.AlwaysTemplate

Then you can set the tintColor on the UITabBar to change the color of the icons and the text simultaneously. 然后,您可以在UITabBar上设置tintColor ,以同时更改图标和文本的颜色。 You should still be able to override that for the text with the appearance selectors you have created though. 不过,您仍然应该可以使用创建的外观选择器覆盖文本的内容。

If I recall, UITabBar should set the renderingMode automatically, so try just setting the tintColor first. 如果我还记得的话, UITabBar应该自动设置renderingMode ,所以尝试先设置一下tintColor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM