改变iOS选项卡栏项目图片和文字颜色

84
这是我的选项卡栏: enter image description here 下图显示了程序运行时选择了“NEWS”项目: enter image description here 显然,“bar tint color”的效果很好!
但是,“tintColor”仅影响图像,而不影响文本。
此外,当选择某个项目时(如上图中的新闻),该项目颜色变为蓝色!我该如何防止这种情况发生?我希望它保持白色。
为什么在选择项目时文本会变成白色,而在未选择时则不会呢?
基本上,我希望项目颜色和文本颜色始终为白色。
我该如何实现这一点?感谢任何帮助。
是否需要Swift代码来处理每个条目?
编辑: enter image description here

你能为所有图标创建白色和灰色的图片,并在需要时进行更改吗? - Max
当选中图片时图片会变蓝,未被选中的文本是白色。我不知道为什么......这是我的问题。 - Greg Peckory
25个回答

4

对于Swift 4.0,现在已经更改为:

tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.gray], for: .normal)
tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.blue], for: .selected)

如果您只需要更改文本颜色,则无需子类化UITabBarItem。只需将上述代码放入视图控制器的viewDidLoad函数中即可。
要进行全局设置更改,请将tabBarItem更改为UITabBarItem.appearance()

4

在Swift 4.2中:

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .selected)

1
我把它放到AppDelegate里面,在func application下面。运行得非常好! - Steffo Dimfelt

4

Swift 3.0

我创建了标签栏类文件并编写以下代码:

viewDidLoad 中:

self.tabBar.barTintColor = UIColor.white
self.tabBar.isTranslucent = true

let selectedColor   = UIColor.red
let unselectedColor = UIColor.cyan

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: unselectedColor,NSFontAttributeName: UIFont(name: "Gotham-Book", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: selectedColor,NSFontAttributeName: UIFont(name: "Gotham-Book", size: 10)!], for: .selected)

if let items = self.tabBar.items {
    for item in items {
        if let image = item.image {
            item.image = image.withRenderingMode( .alwaysOriginal )
            item.selectedImage = UIImage(named: "(Imagename)-a")?.withRenderingMode(.alwaysOriginal)
        }
    }
}

viewDidLoad之后:


   override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

   if(item.title! == "title")
   {
    item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)

    }
    if(item.title! == "title")
    {
        item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)

    }
    if(item.title! == "title")
    {
        item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)

    }
    if(item.title! == "title")
    {
        item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)

    }
    if(item.title! == "title")
    {
        item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)

    }

}

在`ViewDidLoad`方法中,您需要设置所选图片并使用RenderingMode显示其他图片。在tab bar代理方法中,您需要根据标题设置所选图片。

3

您可以设置UIBarItem的tintColor:

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.magentaColor()], forState:.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected)

2
您也可以通过以下方式实现:
override func viewWillLayoutSubviews() {  
  if let items = self.tabBar.items {
    for item in 0..<items.count {
      items[item].image = items[item].image?.withRenderingMode(.alwaysOriginal)
            items[item].selectedImage = items[item].selectedImage?.withRenderingMode(.alwaysTemplate)
    }

可选:

 UITabBar.appearance().tintColor = UIColor.red

我希望你能够受益。

2

如果您想在Tab Bar Item被按下时更改图像,此代码适用于Swift 4。将其复制并粘贴到项目中首个触发的viewDidLoad方法中即可。

   let arrayOfImageNameForSelectedState:[String] = ["Image1Color", "Image2Color","Image3Color"]
   let arrayOfImageNameForUnselectedState: [String] = ["Image1NoColor","Image2NoColor","Image3NoColor"]


    print(self.tabBarController?.tabBar.items?.count)

    if let count = self.tabBarController?.tabBar.items?.count {
        for i in 0...(count-1) {
            let imageNameForSelectedState   = arrayOfImageNameForSelectedState[i]
            print(imageNameForSelectedState)
            let imageNameForUnselectedState = arrayOfImageNameForUnselectedState[i]
            print(imageNameForUnselectedState)
            self.tabBarController?.tabBar.items?[i].selectedImage = UIImage(named: imageNameForSelectedState)?.withRenderingMode(.alwaysOriginal)
            self.tabBarController?.tabBar.items?[i].image = UIImage(named: imageNameForUnselectedState)?.withRenderingMode(.alwaysOriginal)
        }
    }

2
如果您想支持iOS 13及以上版本,请尝试使用以下代码,因为从iOS 13开始设置UItabBar的方法已经完全改变。
        if #available(iOS 13, *) {
            let appearance = UITabBarAppearance()
            
//            appearance.backgroundColor = .white
            appearance.shadowImage = UIImage()
            appearance.shadowColor = .white
            
            appearance.stackedLayoutAppearance.normal.iconColor = .gray
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray]
//            appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .yellow
            
            appearance.stackedLayoutAppearance.selected.iconColor = .systemPink
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemPink]
            
            // set padding between tabbar item title and image
            appearance.stackedLayoutAppearance.selected.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
            appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
            
            self.tabBar.standardAppearance = appearance
        } else {
            // set padding between tabbar item title and image
            UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
            UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.gray], for: .normal)
            UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.systemPink], for: .selected)
        }

2

年份:2020 iOS 13.3

将以下代码复制到AppDelegate.swift -> func didFinishLaunchingWithOptions

//Set Tab bar text/item fonts and size
let fontAttributes = [NSAttributedString.Key.font: UIFont(name: "YourFontName", size: 12.0)!]
UITabBarItem.appearance().setTitleTextAttributes(fontAttributes, for: .normal)
//Set Tab bar text/item color
UITabBar.appearance().tintColor = UIColor.init(named: "YourColorName")

1

这里开始。

每个选项卡栏目都有一个标题、选中图像、未选中图像和徽章值。

使用Image Tint(selectedImageTintColor)字段指定当选中该选项卡时,选项卡项目的色调颜色。默认情况下,该颜色为蓝色。


4
链接已失效(404)。 - Maximelc

1
自从 Xcode 13.0 版本以后,你可以在 UI 上设置这个颜色: 选择标签栏,然后在检查器中自定义“标准”和“滚动到边缘”外观,下面会有堆叠和内联自定义选项。如果你选择自定义,那么你将拥有“标题颜色”设置。你需要全部设置(4个)。

Xcode inspector


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接