简体   繁体   English

如何更改状态栏颜色?

[英]How to change status bar color?

In my app, I am using an image as the background for my ViewController's .在我的应用程序中,我使用图像作为ViewController's . For the status bar in the project settings I set: Status Bar Style - Default .对于我设置的项目设置中的Status Bar Style - DefaultStatus Bar Style - Default I don't use anything else for the status bar.我不使用其他任何东西作为状态栏。

The problem is when the iOS dark mode is enabled my status bar goes white.问题是当启用 iOS 暗模式时,我的status bar变为白色。 And I need it to stay black.我需要它保持黑色。 How to fix it?如何解决?

Also I don't want to turn off iOS dark/light mode supporting in the app.此外,我不想关闭应用程序中支持的 iOS 暗/亮模式。 So the Appearance Light in the Info.plist doesn't quite work for me.所以Info.plist中的Appearance Light对我来说不太适用。

Set your status bar style to dark content:将状态栏样式设置为深色内容:

在此处输入图片说明

After that add in your info.plist View controller-based status bar appearance and set it to NO之后添加您的 info.plist查看基于控制器的状态栏外观并将其设置为NO

在此处输入图片说明

UPDATE更新

if you want dark content only in determinate controller add setNeedsStatusBarAppearanceUpdate in viewWillAppear and after that override preferredStatusBarStyle:如果您只想要确定控制器中的暗内容,请在 viewWillAppear 中添加 setNeedsStatusBarAppearanceUpdate,然后覆盖 preferredStatusBarStyle:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
    if #available(iOS 13.0, *) {
        return .darkContent
    } else {
        return .default
    }

Begin with navigation Controller:从导航控制器开始:

In your Scene delegate declare your first navigation controller:在您的场景委托中声明您的第一个导航控制器:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
    guard let windowScene = (scene as? UIWindowScene) else { return }
    window = UIWindow(windowScene: windowScene)
    window?.makeKeyAndVisible()
    let controller = UINavigationController(rootViewController: FirstViewController())
    controller.navigationBar.barStyle = .black
    window?.rootViewController = controller
}

in SecondViewController override the status bar style在 SecondViewController 中覆盖状态栏样式

override var preferredStatusBarStyle: UIStatusBarStyle {
    if #available(iOS 13.0, *) {
        return .darkContent
    } else {
        return .default
    }
}

For each ViewController you can set status bar color with simple override method.对于每个ViewController您可以使用简单的覆盖方法设置状态栏颜色。

override var preferredStatusBarStyle: UIStatusBarStyle {
    if #available(iOS 13, *) {
        return .darkContent
    } else {
        return .default
    }
}

Don't forget to set View controller-based status bar appearance to YES in your Info.plist.不要忘记在 Info.plist 中将基于视图控制器的状态栏外观设置YES

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

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