【问题标题】:Setting of different text colors for different UITabBarItem's in iOS 15在 iOS 15 中为不同的 UITabBarItem 设置不同的文本颜色
【发布时间】:2021-11-19 20:45:27
【问题描述】:

更新到 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
    }

这适用于 iOS 15 及更早版本。

但在我的项目中,我需要为选项卡栏项目之一设置选定/未选定的文本颜色,与其他项目不同。在运行时设置。

有 5 个标签栏项目。在某些时候,我需要这种行为:其中四个应该具有蓝色/黑色文本颜色(用于选定/未选定状态),一个应该具有红色/绿色。

在 iOS 15 之前,我使用此代码在每一刻设置所需项目的颜色:

let indexOfItemToChange = 4
tabBar.items[indexOfItemToChange].setTitleTextAttributes([.foregroundColor: UIColor.red], for: .selected)
tabBar.items[indexOfItemToChange].setTitleTextAttributes([.foregroundColor: UIColor.green], for: .normal)

更新到 iOS 15 后,它不起作用。我试过这样设置:

let indexOfItemToChange = 4
tabBar.items[indexOfItemToChange].standardAppearance?.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.green]
tabBar.items[indexOfItemToChange].standardAppearance?.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: UIColor.red]

但它也没有效果。即使在设置标签栏项目颜色之前我为每个标签栏项目设置了 UITabBar 外观:

tabBar.items.forEach {
$0.standardAppearance = standardAppearance
$0.scrollEdgeAppearance =  scrollEdgeAppearance
}

有人遇到过这个问题吗?有什么建议吗?

PS。在我的项目中,没有 xib 和故事板。所以我只需要以编程方式解决我的问题。

【问题讨论】:

    标签: ios swift ios15 xcode13


    【解决方案1】:

    我们可以使用这段代码来改变 TabBar 项目的外观:-

      if #available(iOS 15.0, *) {
            
            let tabBarAppearance = UITabBarAppearance()
            let tabBarItemAppearance = UITabBarItemAppearance()
            
            tabBarAppearance.backgroundColor = .white
            
            tabBarItemAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: Constants.Color.appDefaultBlue]
            tabBarItemAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
            
            tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
            tabBar.standardAppearance = tabBarAppearance
            tabBar.scrollEdgeAppearance = tabBarAppearance
            
        }
    

    请确保我们仅在 TabBar 类中使用此代码以获得所需的结果。

    【讨论】:

    • 对不起,你有没有看我的问题?问题不在于如何为所有标签栏项目设置选定/未选定的颜色。关于如何为不同的标签栏项目设置不同的选定/未选定颜色的问题。例如第一个为绿色/红色,第二个为黑色/黄色等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    相关资源
    最近更新 更多