问题
How do I update or reload the text color on the UITabBarItem?
It will, only if I kill the app and re-open again. Then it will refresh the textColor on the UITabBarItem
Swift 5.1, iOS 12
func handleRefreshForTabBar() {
DispatchQueue.main.async {
//Background
self.view.backgroundColor = Theme.current.generalBackground
//Images
self.tabBar.tintColor = Theme.current.tabTintColor
//Bar
self.tabBar.barTintColor = Theme.current.tabBarTintColor
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
handleRefreshForTabBar()
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: Theme.current.tabBarSelectedTextColor], for: .selected)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: Theme.current.tabBarNormalTextColor], for: .normal)
}
Options that I've tried
tabBar.setNeedsDisplay()
tabBar.setNeedsLayout()
view.reloadInputViews()
view.setNeedDisplay()
view.setNeedsLayout()
My TabBar is my rootVC
回答1:
/// Function that makes the tab bar refresh itself. Specially important for refreshing light or dark mode styles
func handleRefreshForTabBar() {
DispatchQueue.main.async {
//Set up the same color as the NavBar to avoid bugs
self.view.backgroundColor = Theme.current.generalBackground
//Images
self.tabBar.tintColor = Theme.current.tabTintColor
//Bar
self.tabBar.barTintColor = Theme.current.tabBarTintColor
self.tabBar.items!.forEach { (item) in
item.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: Theme.current.tabBarSelectedTextColor], for: .selected)
item.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: Theme.current.tabBarNormalTextColor], for: .normal)
}
}
}
来源:https://stackoverflow.com/questions/58942737/uitabbarcontroller-uitabbaritem-updating-it-or-reloading-it