更改UITabBar图标的颜色

时间:2014-12-13 08:00:34

标签: ios swift uitabbarcontroller uitabbar uicolor

我想将UI标签栏图标颜色更改为以下值:红色:0.75,绿色:1.0,蓝色:1.0,Alpha:1.0,未选择图标时为白色。

直到现在我才认为你是这样做的:

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)

上面的代码在我的代表中。

现在我已经输入了这段代码:

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.
}



}

进入我的标签栏视图控制器cocco触摸文件。我不知道为什么我仍然会收到错误

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

如果tabbaritem有图像,您可以通过以下方式设置其颜色:

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

答案 2 :(得分:0)

我认为无法使用它,您只需获取所需颜色的图像并在UITabbar中使用即可 但是要更改所有TabBar颜色,可以在Func的AppDelegate中使用以下颜色:

didFinishLaunchingWithOptions

要添加的行:

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) 

注意:您可以根据自己的应用更改颜色

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

祝您编码愉快

相关问题