在导航控制器中时,视图控制器的首选状态栏样式被忽略

Preferred status bar style of view controller is ignored when in navigation controller(在导航控制器中时,视图控制器的首选状态栏样式被忽略)

本文介绍了在导航控制器中时,视图控制器的首选状态栏样式被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个具有多个视图的 iOS 应用程序.我已将 App 设置为使用基于 ViewController 的状态栏样式,这允许我使用以下代码

I'm writing an iOS App with multiple views. I've set the App to use ViewController-based status bar style, which allows me to use the following code

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
} 

效果如预期.

然后我将视图嵌入到导航控制器中,并将 BarButtonItem 与 showSegue 连接起来.从此视图的ViewController切换到忽略样式设置,默认显示黑色状态栏.

But then I've embedded the views in a navigation controller and connected a BarButtonItem with a showSegue. Since then the ViewController of the view switched to ignores the style settings and shows the default black status bar.

推荐答案

当你在一个不会被调用的导航控制器中时.导航控制器的 preferredStatusBarStyle 将被调用.与您的代码一起尝试:

When you're in a navigation controller that will not get called. The navigation controller's preferredStatusBarStyle will be called. Try this along with your code:

extension UINavigationController {

   open override var preferredStatusBarStyle: UIStatusBarStyle {
      return topViewController?.preferredStatusBarStyle ?? .default
   }
}

这篇关于在导航控制器中时,视图控制器的首选状态栏样式被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在导航控制器中时,视图控制器的首选状态栏样式被忽略

基础教程推荐