简体   繁体   English

如何更改标签栏中未选中项目的颜色?

[英]How do you change the color of unselected items in a Tab Bar?

I'm trying to change the color of the unselected item images in my Tab Bar from the default gray color to the actual color of the image (white), but my efforts have been unsuccessful.我正在尝试将标签栏中未选中项目图像的颜色从默认的灰色更改为图像的实际颜色(白色),但我的努力没有成功。 There are several threads related to this question on Stack Overflow, but many of them seem to be outdated. Stack Overflow上有几个与这个问题相关的线程,但其中许多似乎已经过时了。

Tab Bar with Home and Profile icons带有主页和个人资料图标的标签栏

As you can see from the picture, the unselected Profile icon on the right has a default gray color.从图片中可以看出,右侧未选中的 Profile 图标默认为灰色。 After doing some research, I've tried running the solution below in the App Delegate file to change its color to white, but sadly this hasn't worked.在做了一些研究之后,我尝试在 App Delegate 文件中运行以下解决方案,将其颜色更改为白色,但遗憾的是这并没有奏效。 I'm a novice Swift programmer (incidentally, this is also my first post), so any help or suggestions would be greatly appreciated.我是一名新手 Swift 程序员(顺便说一句,这也是我的第一篇文章),所以任何帮助或建议将不胜感激。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    let tabBarController = UITabBarController()
    tabBarController.tabBar.unselectedItemTintColor = .white

    return true
}

Make the icon images Black, if they are white Im not sure it will work.将图标图像设为黑色,如果它们是白色的,我不确定它是否会起作用。 As in the actual image themselves should be black in the assets folder not white.与实际图像一样,资产文件夹中的图像本身应该是黑色而不是白色。

If you add a tabbar from the story board you can put these line of code in that method in the appDelegate method shown in your post如果您从故事板添加标签栏,您可以将这些代码行放在您帖子中显示的 appDelegate 方法中的该方法中

UITabBar.appearance().barTintColor = UIColor.black
UITabBar.appearance().tintColor = UIColor.red
UITabBar.appearance().unselectedItemTintColor = .white

You can try to use UITabBar.appearence().您可以尝试使用 UITabBar.appearence()。 unselectedItemTintColor, more information here https://developer.apple.com/documentation/uikit/uitabbar/legacy_customizations unselectedItemTintColor,更多信息在这里https://developer.apple.com/documentation/uikit/uitabbar/legacy_customizations

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

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