繁体   English   中英

当在 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

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

两个选中的和未选中的项目图标都有高亮颜色

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

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

iOS 13 中有一个新的外观 API。要使用 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

    } 

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

self.tabBar.unselectedItemTintColor = [UIColor lightGrayColor];

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

使用IB中的属性字段“Image Tint”

苹果自己的Podcasts App也有同样的问题。 当前是一个错误。

对于 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
    }
self.tabBarController?.tabBar.unselectedItemTintColor = UIColor.lightGray

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

所以它看起来像这样

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

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

感谢 Samuël 的回答。 这是我的应用程序中的 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)
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM