首页 > 解决方案 > 如何更改状态栏的背景颜色?

问题描述

我不想在我的应用程序中使用半透明的导航栏。所以,我添加了这段代码来改变它。它有效,但有一个小问题。如果我使用 largeTitle 导航,状态栏颜色看起来是灰色的。我该如何处理?提前致谢。

UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().backgroundColor = UIColor.white

截屏

标签: iosswiftui

解决方案


您可以通过使用在状态栏顶部添加子视图来修复它UIStatusBarManager

if #available(iOS 13, *) {
    let keyWindow = UIApplication.shared.connectedScenes
        .filter({$0.activationState == .foregroundActive})
        .map({$0 as? UIWindowScene})
        .compactMap({$0})
        .first?.windows
        .filter({$0.isKeyWindow}).first
    let statusBar = UIView(frame: (keyWindow?.windowScene?.statusBarManager?.statusBarFrame)!)
    statusBar.backgroundColor = .white
    keyWindow?.addSubview(statusBar)
}

推荐阅读