【问题标题】:Change color of UITabBar Icon更改 UITabBar 图标的颜色
【发布时间】:2015-02-11 22:33:52
【问题描述】:

我想将 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 touch 文件。我不知道为什么我仍然收到错误

【问题讨论】:

  • 是否有您想要着色的图像,或者只是文本?
  • 你设置了tint color
  • 我目前没有图像,但我正在谈论未来。我可以在“更多”标签栏图标中看到它是灰色的,并且与我的蓝色背景不匹配。 @DánielNagy
  • 那我该如何设置图标的颜色呢? @soulshined
  • stackoverflow.com/questions/10755576/… 这是一个客观的 c 示例,但请看第二个答案,而不是接受的答案

标签: ios swift uitabbarcontroller uitabbar uicolor


【解决方案1】:

我已经遍历了UITabbarController的所有子控制器,然后设置了TabBarItem的颜色或者每个控制器的tint颜色,就可以了。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我在哪里添加这个?在委托或实际的视图控制器中?
    • 在实际的视图控制器中,例如在 viewDidLoad 函数中
    • 哪个视图控制器?我为标签栏导航控制器制作的那个?还是任何屏幕?
    • 最好的办法是放在为标签栏创建的那个上,但这并不重要。
    • 我收到一条错误消息:Use of unresolved identifier 'testImageView'
    【解决方案3】:

    我认为它无法制作,您可以获取所需颜色的图像并在 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

    祝你编码愉快

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 2014-03-03
      • 1970-01-01
      • 2010-10-21
      • 1970-01-01
      • 2014-11-24
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多