简体   繁体   English

iOS 13 状态栏样式

[英]iOS 13 status bar style

I want to change the status bar style on a per-ViewController level on iOS 13. So far I didn't have any luck.我想在 iOS 13 的每个 ViewController 级别上更改状态栏样式。到目前为止,我没有任何运气。
I define UIUserInterfaceStyle as Light in info.plist (as I do not want to support dark mode) and set UIViewControllerBasedStatusBarAppearance to true .我在 info.plist UIUserInterfaceStyle定义为Light (因为我不想支持暗模式)并将UIViewControllerBasedStatusBarAppearance设置为true preferredStatusBarStyle is called on my ViewController but completely ignored. preferredStatusBarStyle在我的 ViewController 上被调用,但完全被忽略。 The UIUserInterfaceStyle seems to always override the VC preferences. UIUserInterfaceStyle似乎总是覆盖 VC 首选项。 How do I get per-ViewController status bar style working on iOS 13?如何让每个 ViewController 状态栏样式在 iOS 13 上工作? Or is it not supported any more?还是不再支持?

iOS 13.2, Swift 5.1 iOS 13.2,斯威夫特 5.1

For me nothing worked from solutions mentioned before.对我来说,之前提到的解决方案没有任何效果。 After 5 hours I ended up on modalPresentationCapturesStatusBarAppearance flag . 5 小时后,我结束了modalPresentationCapturesStatusBarAppearance flag 。

    destinationNavigationController.modalPresentationCapturesStatusBarAppearance = true
    sourceViewController.present(destinationNavigationController, animated: animated, completion: nil)

After this preferredStatusBarStyle was called in presented VC.呈现的VC 中调用了这个preferredStatusBarStyle之后。

override var preferredStatusBarStyle: UIStatusBarStyle {
    if #available(iOS 13.0, *) {
        if traitCollection.userInterfaceStyle == .light {
            return .darkContent
        } else {
            return .lightContent
        }
    } else {
        return .lightContent
    }
}

I had the same issue on iOS13 while it was fine on iOS12 for my app.我在 iOS13 上遇到了同样的问题,而在 iOS12 上我的应用程序没问题。 I have a TabBarController which holds 3 NavigationBarControllers, and I present TabBarController from a previous ViewController.我有一个包含 3 个 NavigationBarController 的 TabBarController,并且我从以前的 ViewController 中呈现 TabBarController。 I fixed it by setting .modalPresentationStyle to .fullScreen when presenting:我通过在演示时将 .modalPresentationStyle 设置为 .fullScreen 来修复它:

tabbarController.modalPresentationStyle = .fullScreen

Maybe it will help you somehow...也许它会以某种方式帮助你......

In iOS13, there is now a .darkContent option for UIStatusBarStyle.在 iOS13 中,UIStatusBarStyle 现在有一个 .darkContent 选项。 For black text, use this (instead of .default) for preferredStatusBarStyle.对于黑色文本,使用这个(而不是 .default)作为 preferredStatusBarStyle。

In my case, I had a similar issue with incorrect UIStatusBarStyle .就我而言,我遇到了类似的问题,不正确的UIStatusBarStyle For some view controllers in my app, I need to set a dark status bar style ignoring current system color mode.对于我的应用程序中的某些视图控制器,我需要设置一个深色状态栏样式而忽略当前系统颜色模式。 The problem was I used .default value, but in iOS 13 it changes depending on the context.问题是我使用了.default值,但在 iOS 13 中它会根据上下文发生变化。 That's why I added a small workaround to handle both iOS 12- and iOS 13+ cases.这就是为什么我添加了一个小的解决方法来处理 iOS 12- 和 iOS 13+ 的情况。

private extension UIStatusBarStyle {

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

}

i had the same problem on iOS 13 while using navigation controller i was able to change the status bar color using我在使用导航控制器时在 iOS 13 上遇到了同样的问题,我能够使用更改状态栏颜色

let navBarAppearance = UINavigationBarAppearance()让 navBarAppearance = UINavigationBarAppearance()

navigationBar.scrollEdgeAppearance = navBarAppearance navigationBar.scrollEdgeAppearance = navBarAppearance

but the problem was when i was using present controller the status bar didn't change i used this code on top view controller但问题是当我使用当前控制器时状态栏没有改变我在顶视图控制器上使用了这个代码

override func viewDidAppear(_ animated: Bool) {覆盖 func viewDidAppear(_ 动画:布尔){

    if #available(iOS 13, *)
    {
        let statusBar = UIView(frame: (UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame)!)
        statusBar.backgroundColor = UIColor.systemBackground
        UIApplication.shared.keyWindow?.addSubview(statusBar)
    }
}

you can use your color in "UIColor.systemBackground"您可以在“UIColor.systemBackground”中使用您的颜色

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

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