小编Dmy*_*nko的帖子

iOS 15 中不同 UITabBarItem 的不同文本颜色设置

更新到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
    }
Run Code Online (Sandbox Code Playgroud)

这适用于 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)
Run Code Online (Sandbox Code Playgroud)

更新到iOS 15后就没有效果了。我尝试过这样设置: …

ios swift ios15 xcode13

5
推荐指数
0
解决办法
2236
查看次数

标签 统计

ios ×1

ios15 ×1

swift ×1

xcode13 ×1