iOS 13 中的 UITabBar 透明标签错误

UITabBar transparent labels bug in iOS 13

iOS: 13.1.2 Xcode: 11.1 (11A1027)

在我们的标签栏中,我们选择为标签项使用透明文本,因此在 iPhone 中,我们只显示标签项图像,而文本是不可见的(它应该只在 iPad), 我们通过调用:

extension UITabBarItem {
    func updateTitleVisibility(for traitCollection: UITraitCollection) {
        switch traitCollection.horizontalSizeClass {
        case .compact:
            hideTabBarTitle()
        default:
            showTabBarTitle()
        }
    }

    func hideTabBarTitle() {
        imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
        setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)
        setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .selected)
    }

    func showTabBarTitle() {
        imageInsets = .zero
        setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.licorice], for: .normal)
        setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.secondaryBlue], for: .selected)
    }
}

在为 iOS 13 编译我们的应用程序时(在 iOS 12 上没有发生),发生了一个奇怪的行为(注意标签栏):

Video of the bug @ Imgur

(^ 我没能把它嵌入 post)

因此在呈现全屏视图控制器后,非活动选项卡的选项卡文本突然显示,但在检查视图调试器时令人难以置信 应该透明的标签是 确实透明

有人见过这样的行为吗?我该如何解决

好吧,这是由于 iOS 13.

上默认黑暗模式下的行为所致

要在 iOS 版本低于 13 时使用标签实现您想要的效果,

只需将此添加到您的 Info.plist:

<key>UIUserInterfaceStyle</key>
<string>Light</string>

这实际上是将全局用户界面样式更改为浅色样式,这是 iOS 13 以下版本的默认样式。

如果您不想更改用户界面样式,您还可以更改选项卡栏上未选中项目的色调:

tabBar.unselectedItemTintColor = .darkGray

或您选择的任何其他色调。