【问题标题】:preferredStatusBarStyle not getting called in iOS 13 and otherpreferredStatusBarStyle 在 iOS 13 和其他版本中没有被调用
【发布时间】:2020-04-21 23:26:31
【问题描述】:

我的应用程序中有多个UITabBar,有些ViewController 有白色状态栏,有些ViewController 有黑色状态栏。

我的info.plist

View controller-based status bar appearanceYES

我的 Viewcontroller 有以下代码。

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .default //or return . lightContent
}

preferredStatusBarStyle 永远不会被调用。

我还在我的控制器viewDidLoad 中写了下面的行,但上面仍然没有被调用。

self.setNeedsStatusBarAppearanceUpdate()

我也已将controller-based status bar appearance 更改为YES && NO 多次检查,但对我没有任何帮助。

我也尝试了以下解决方案和其他 stackoverflow 答案,但没有任何帮助。

preferredStatusBarStyle not respecting on iOS 13

preferredStatusBarStyle var not working in iOS12?

编辑

我尝试了下面的代码,它返回了topViewController,它将调用preferredStatusBarStyleViewController

extension UINavigationController {
    override open var childForStatusBarStyle: UIViewController? {
        return topViewController
    }
}

所以一旦topViewController 找到它就会调用那个特定ViewControllerpreferredStatusBarStyle

但问题是它没有在 UITabBarController -> UINavigationController -> UIViewController 内部被调用。

要求

我有 2 个不同的TabBarController

第一个TabBarController statusBarStyle 是.lightContent

第二个TabBarController statusBarStyle 是.lightContent.default 在不同的控制器中。

当我更改为第二个 TabBarController 时,它将调用第二个 TabBarControllerpreferredStatusBarStyle 和所有 ViewController statusBarStyle 去 .default 但我的一些控制器 statusBarStyle 想要成为 .ligthContent

我怎样才能做到这一点?

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 我仍然使用 UIApplication.shared.statusBarStyle 虽然它已被弃用,但它仍然可以正常工作,在 viewWillAppear 中调用它
  • @AlexandrKolesnik,好吧,让我试试吧。
  • @AlexandrKolesnik,我的流程是这样的。 TabBarController--> NavigationController--> ViewController.
  • @AlexandrKolesnik,我把上面的行放在ViewControllerviewWillAppear 然后适当的状态栏样式显示1秒钟,然后它会自动转到.light。
  • 从 plist 中删除 controller-based status bar appearance 在 vi​​ewWillAppear 中调用 UIApplication.shared.statusBarStyle = .default setNeedsStatusBarAppearanceUpdate()

标签: ios swift statusbar ios13


【解决方案1】:

这与 iOS 13 无关。只是规则错误。

在导航控制器的情况下,状态栏的颜色不是由视图控制器的preferredStatusBarStyle 决定的。

令人惊讶的是,它是由导航栏的 barStyle 决定的。要获取浅色状态栏文本,请说(在您的视图控制器中):

self.navigationController?.navigationBar.barStyle = .black

【讨论】:

    【解决方案2】:

    我得到了解决方案。

    在下面输入代码以找到topViewController

    extension UINavigationController {
        override open var childForStatusBarStyle: UIViewController? {
            return topViewController
        }
    }
    

    所以一旦找到topViewController,下面的代码就会在你当前的ViewController 中被调用,你可以根据需要设置statusBarStyle

    override var preferredStatusBarStyle: UIStatusBarStyle { }
    

    就我而言,我有 2 个TabBar

    第一个TabBar 控制器属于.lightContent,第二个TabBar 控制器属于.default,因此创建2 个UITabBarController。第一个用于.lightContent,第二个用于.default,并将preferredStatusBarStyle 放入其中。

    所以当你在 UITabBarController 子控制器时,你的 UITabBarController preferredStatusBarStyle 被调用并且子控制器 statusBarStyle 按照你设置的样式设置。

    【讨论】:

      【解决方案3】:

      请参考这里

      https://developer.apple.com/documentation/uikit/uiviewcontroller/1621453-modalpresentationcapturesstatusb

      override var modalPresentationCapturesStatusBarAppearance: Bool {
              set {}
              get{
      
                  return true
              }
          }
      

      【讨论】:

      • 这将给出一个错误,例如:Cannot override mutable property with read-only property 'modalPresentationCapturesStatusBarAppearance'
      • 我已经更新了,我不知道它是如何错过你需要调用 set 并同时获取两者的。
      • 请您编写示例代码,例如如何通过此更改状态栏样式?
      • 你写了上面的方法并被调用了吗?在preferredStatusBarStyle 不起作用的地方。如果您检查,您将看到不会调用preferredsta ytsubarstyle 的情况是presentation case。如果它不起作用,可以给我发送代码或屏幕截图,我可以帮助你。
      【解决方案4】:

      the following may be of assistance to you;

      有关开发人员指南,请参阅 UIStatusBarStyle 常量 UIApplicenter code hereationpreferredStatusBarStyle UIViewController 中的属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-18
        • 1970-01-01
        相关资源
        最近更新 更多