[英]Setting of different text colors for different UITabBarItem's in iOS 15
After update to iOS 15, I implemented UITabBar configuration this way:更新到iOS 15后,我这样实现了UITabBar配置:
let backgroundColor = UIColor.grey
let selectedItemTextColor = UIColor.blue
let unselectedItemTextColor = UIColor.black
if #available(iOS 15, *) {
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.backgroundColor = backgroundColor
tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: selectedItemTextColor]
tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: unselectedItemTextColor]
tabBar.standardAppearance = tabBarAppearance
tabBar.scrollEdgeAppearance = tabBarAppearance
} else {
UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: selectedItemTextColor], for: .selected)
UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: unselectedItemTextColor], for: .normal)
tabBar.barTintColor = backgroundColor
}
This works fine for iOS 15 and older versions.这适用于 iOS 15 及更旧版本。
But in my project I need to set selected/unselected text color for one of tab bar items, different from other items.但是在我的项目中,我需要为选项卡栏项目之一设置选定/未选定的文本颜色,这与其他项目不同。 Set it in runtime.
在运行时设置它。
There are 5 tab bar items.有 5 个选项卡栏项目。 In some moment, I need this behavior: four of them should have blue/black text color (for selected/unselected states) and one should have red/green color.
在某些时候,我需要这种行为:其中四个应该有蓝色/黑色文本颜色(对于选定/未选定状态),一个应该有红色/绿色。
Until iOS 15, I used this code to set colors of needed item in every moment of time:直到iOS 15,我用这段代码设置了每时每刻所需物品的colors:
let indexOfItemToChange = 4
tabBar.items[indexOfItemToChange].setTitleTextAttributes([.foregroundColor: UIColor.red], for: .selected)
tabBar.items[indexOfItemToChange].setTitleTextAttributes([.foregroundColor: UIColor.green], for: .normal)
After update update to iOS 15 it makes no effect. update 更新到 iOS 15 后没有效果。 I have tried to set it like this:
我试过这样设置:
let indexOfItemToChange = 4
tabBar.items[indexOfItemToChange].standardAppearance?.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.green]
tabBar.items[indexOfItemToChange].standardAppearance?.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: UIColor.red]
But it makes no effect too.但它也没有效果。 Even if before setting of tab bar item colors I set UITabBar appearances for each tab bar item:
即使在设置标签栏项目 colors 之前,我为每个标签栏项目设置了 UITabBar 外观:
tabBar.items.forEach {
$0.standardAppearance = standardAppearance
$0.scrollEdgeAppearance = scrollEdgeAppearance
}
Some one faced this issue?有人遇到过这个问题吗? Any advice?
有什么建议吗?
PS.附言。 In my project there are no xibs and storyboards.
在我的项目中没有 xibs 和故事板。 So I need to solve my problem only programmatically.
所以我只需要以编程方式解决我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.