当在 Xcode 11,beta 2 的 Interface Builder 中指定条形色调颜色时,UITabBarItem 图标未正确着色 iOS 13

UITabBarItem icon not colored correctly for iOS 13 when a bar tint color is specified in Interface Builder in Xcode 11, beta 2

当我 运行 在 iOS 13 个模拟器上使用 Xcode 11,beta 2 时,我的 UITabBarItems 的颜色有问题。我从头开始制作了一个示例项目,并且当我没有指定条形色调颜色时一切正常。但是,当我通过 Interface Builder 指定自定义条形色调颜色时,我得到:

如果我在 Interface Builder 中将 "Bar Tint" 属性 设置为不清晰,则选项卡栏中的所有项目图标都具有选定的颜色。当它设置为清除时,图标会正确着色。如果我在 iOS 12 模拟器中编译和 运行,图标也会正确着色。

这似乎是 Xcode 11 中的错误,但也许我遗漏了什么?

从表面上看,这似乎是一个错误,但是您可以通过在 UITabBar 实例上定义 .unselectedItemTintColor 来缓解它。

self.tabBar.unselectedItemTintColor = [UIColor lightGrayColor];
self.tabBarController?.tabBar.unselectedItemTintColor = UIColor.lightGray

这在 swift 中对我有用 4. 只需将它放在 override func viewWillDisappear(_ animated: Bool) 方法中,它就会随着视图的变化而更新。

所以它看起来像这样

override func viewWillDisappear(_ animated: Bool) {
    self.tabBarController?.tabBar.unselectedItemTintColor = UIColor.lightGray
}

即使这是一个错误(我不确定),您也可以使用它来使用您选择的任何颜色来更改选项卡栏项目的颜色

API 在 iOS 13 中有一个新的外观。要使用 Xcode 11.0 正确地为标签栏项目的图标和文本着色,您可以这样使用它:

    if #available(iOS 13, *) {
        let appearance = UITabBarAppearance()

        appearance.backgroundColor = .white
        appearance.shadowImage = UIImage()
        appearance.shadowColor = .white

        appearance.stackedLayoutAppearance.normal.iconColor = .black
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
        appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .blue

        appearance.stackedLayoutAppearance.selected.iconColor = .red
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

        self.tabBar.standardAppearance = appearance

    } 

使用 IB 中的属性字段 "Image Tint"。

对于 Objective C 你可以在 AppDelegate 中使用:

[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.65f]];
    if #available(iOS 13.0, *) {
        let appearance = tabBar.standardAppearance
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]
        appearance.stackedLayoutAppearance.normal.iconColor = UIColor.black
        appearance.stackedLayoutAppearance.selected.iconColor = UIColor.blue
        tabBar.standardAppearance = appearance
    } else {
        tabBar.unselectedItemTintColor = UIColor.black
        tabBar.tintColor = UIColor.blue
    }

感谢塞缪尔的回答。这是我的应用程序中的 UITabBar 设置,它已经是 2021 年了,但在互联网上仍然很少有关于如何为 iOS 13 及更高版本设置 UITabBar 的有用信息。

    if #available(iOS 13, *) {
            let appearance = UITabBarAppearance()
            
//            appearance.backgroundColor = .white
            appearance.shadowImage = UIImage()
            appearance.shadowColor = .white
            
            appearance.stackedLayoutAppearance.normal.iconColor = .gray
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray]
//            appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .yellow
            
            appearance.stackedLayoutAppearance.selected.iconColor = .systemPink
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemPink]
            
            // set padding between tabbar item title and image
            appearance.stackedLayoutAppearance.selected.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
            appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
            
            self.tabBar.standardAppearance = appearance
        } else {
            // set padding between tabbar item title and image
            UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
            UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.gray], for: .normal)
            UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.systemPink], for: .selected)
        }