简体   繁体   English

更改 UITabBar 图标的颜色

[英]Change color of UITabBar Icon

I want to change the UI Tab Bar Icon colour to the values: Red: 0.75, Green: 1.0, Blue: 1.0, Alpha: 1.0 and to white when the Icon is not selected.我想将 UI 选项卡栏图标颜色更改为以下值:红色:0.75、绿色:1.0、蓝色:1.0、Alpha:1.0 并在未选择图标时更改为白色。

Till now I thought this is how you do it:直到现在我认为这就是你的做法:

UITabBarItem.appearance().finishedSelectedImage([NSForegroundColorAttributeName: UIColor(red: 0.75, green: 1.0, blue: 1.0, alpha: 1.0)], forState: UIControlState.Selected)

    UITabBarItem.appearance().finishedUnselectedImage([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: UIControlState.Normal)

The code above is in my delegate.上面的代码在我的委托中。

Now I have put this code:现在我已经把这个代码:

import UIKit

class UITabBarViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    var testImage = UIImage(named: "22274")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    testImageView.tintColor = UIColor.redColor()
    testImageView.image = testImage

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



}

Into my tab bar view controller cocco touch file.进入我的标签栏视图控制器 cocco touch 文件。 I don't know why I'm getting an error still我不知道为什么我仍然收到错误

我已经遍历了UITabbarController的所有子控制器,然后设置了每个控制器的TabBarItem的颜色或色调,它确实有效。

If the tabbaritem has an image, you can set its color by:如果 tabbaritem 有图像,您可以通过以下方式设置其颜色:

var testImage = UIImage(named: "someImageName")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
testImageView.tintColor = UIColor.redColor()
testImageView.image = testImage

I think It's not available to make it, you can just get an image in the color you want and use it in the UITabbar but regarding to change the all TabBar colors you can use the below in your AppDelegate in Func:我认为它无法实现,您可以获取所需颜色的图像并在 UITabbar 中使用它,但是关于更改所有 TabBar 颜色,您可以在 Func 的 AppDelegate 中使用以下内容:

didFinishLaunchingWithOptions

Lines To be added:要添加的行:

UITabBar.appearance().barTintColor = #colorLiteral(red: 0.2000651062, green: 0.1960035861, blue: 0.2000851929, alpha: 1)

UITabBar.appearance().tintColor = #colorLiteral(red: 0.2000651062, green: 0.1960035861, blue: 0.2000851929, alpha: 1)

UITabBar.appearance().unselectedItemTintColor = #colorLiteral(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1) 

Note: You can change colors depending on your App注意:您可以根据您的应用程序更改颜色

Check the image: https://i.stack.imgur.com/wESVH.png检查图像: https : //i.stack.imgur.com/wESVH.png

and have a happy coding day并有一个快乐的编码日

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

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