简体   繁体   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

I have a problem with the color of my UITabBarItems when I run on iOS 13 simulators, using Xcode 11, beta 2. I have made a sample project from scratch, and everything works correctly when I do not specify a bar tint color.当我使用 Xcode 11, beta 2 在 iOS 13 模拟器上运行时,我的 UITabBarItems 的颜色有问题。我从头开始制作了一个示例项目,当我没有指定条形色调颜色时,一切正常。 However, when I do specify a custom bar tint color via Interface Builder, I get this:但是,当我通过 Interface Builder 指定自定义条形色调颜色时,我得到以下信息:

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

All items icons in the tab bar have the selected color if I set the "Bar Tint" property in Interface Builder to anything but clear.如果我将 Interface Builder 中的“Bar Tint”属性设置为不清晰,则选项卡栏中的所有项目图标都具有选定的颜色。 When it is set to clear, the icons are colored properly.当它设置为清除时,图标的颜色正确。 The icons are also colored properly if I compile and run in an iOS 12 simulator.如果我在 iOS 12 模拟器中编译和运行,图标也会正确着色。

This seems like a bug in Xcode 11, but maybe I'm missing something?这似乎是 Xcode 11 中的一个错误,但也许我遗漏了什么?

There is a new appearance API in iOS 13. To color tabbar item's icon and text correctly using Xcode 11.0 you can use it like this: 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];

Use the attribute field "Image Tint" in IB.使用IB中的属性字段“Image Tint”。

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

Apple's own Podcasts App has the same issue. 苹果自己的Podcasts App也有同样的问题。 This is a bug currently. 当前是一个错误。

For Objective C you can use in AppDelegate:对于 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

This works for me in swift 4. Just put this in the override func viewWillDisappear(_ animated: Bool) method and this will update as the view is changing.这在 swift 4 中对我有用。只需将它放在override func viewWillDisappear(_ animated: Bool)方法中,它就会随着视图的变化而更新。

So it will look something like this所以它看起来像这样

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

Even if this is a bug(I'm not sure) you can use this to change the color of the tab bar item by using any color of you choice即使这是一个错误(我不确定),您也可以使用它来使用您选择的任何颜色来更改标签栏项目的颜色

Thanks for Samuël's answer.感谢 Samuël 的回答。 Here is the UITabBar setting in my app, it is 2021 already but still rare helpful information in internet about how to set UITabBar for iOS 13 and above.这是我的应用程序中的 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