问题描述
我写了如下代码。
AppDelegate.swift
func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UITabBarItem.appearance().setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 9,weight: .bold),.foregroundColor: UIColor.init(red: 50/255,green: 50/255,blue: 50/255,alpha: 1)],for: .normal)
return true
}
然后,我转到一个选项卡上的另一个Viewcontoller屏幕。同时,我按照描述的那样编写代码:
nav.modalPresentationStyle = .fullScreen
然后,关闭ViewContoroller屏幕。然后,其他选项卡文本颜色已更改为默认颜色(蓝色)。为什么会这样?
TabBarViewController.swift
override func viewWillAppear(_ animated: Bool) {
let color = UIColor.init(red: 50/255,alpha: 1)
UITabBarItem.appearance().setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 9,.foregroundColor: color],for: .normal)
tabBar.tintColor = color
}
我分享了一个有关该问题的视频,以清楚地向您解释。 请检查它。
解决方法
您没有像这样配置所选颜色:
UITabBarItem.appearance().setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 9,weight: .bold),.foregroundColor: yourcolor],for: .selected)
编辑:
如果您在iOS 13+上运行,则应按以下方式配置UITabBarAppearence:
let tabBarItemAppearence = UITabBarItemAppearance()
tabBarItemAppearence.normal.iconColor = UIColor.yourcolor
tabBarItemAppearence.selected.iconColor = UIColor.yourcolor
tabBarItemAppearence.normal.titleTextAttributes = [.foregroundColor: UIColor.yourcolor]
tabBarItemAppearence.selected.titleTextAttributes = [.foregroundColor: UIColor.yourcolor]
let tabBarAppearence = UITabBarAppearance()
tabBarAppearence.inlineLayoutAppearance = tabBarItemAppearence
tabBarAppearence.compactInlineLayoutAppearance = tabBarItemAppearence
tabBarAppearence.stackedLayoutAppearance = tabBarItemAppearence
tabBarAppearence.backgroundColor = color
然后像这样设置外观
tabBarController.tabBar.standardAppearance = tabBarAppearence