【问题标题】:Why Is My Status Bar Not Changing Preferred Style?为什么我的状态栏没有改变首选样式?
【发布时间】:2017-01-31 03:26:12
【问题描述】:

我在Info.plist 文件中将View controller-based status bar appearance 设置为YES

视图控制器,不在导航堆栈中,通过模态 segue 呈现。在其中,我为 Swift 3/iOS 10 提供了以下属性覆盖:

override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }

不过,状态栏仍保留其默认(深色)样式。但是,当我在viewDidLoad 中打印preferredStatusBarStyle 时,我得到rawValue1。查看documentation1指的是.lightContent

我正在尝试将状态栏的样式更改为.lightContent,因为视图包含UIVisualEffectViewUIBlurEffectStyle 值为dark

我是不是做错了什么?

【问题讨论】:

    标签: ios swift swift3 statusbar


    【解决方案1】:

    在 matt 的 answer 的帮助下,我能够解决我的问题。我不得不在viewDidLoad 中使用以下内容:

    modalPresentationCapturesStatusBarAppearance = true
    

    当我尝试 matt 的解决方案时,我收到了两个编译错误:Cannot override mutable property with read-only property 'modalPresentationCapturesStatusBarAppearance'Getter for 'modalPresentationCapturesStatusBarAppearance' with Objective-C selector 'modalPresentationCapturesStatusBarAppearance' conflicts with getter for 'modalPresentationCapturesStatusBarAppearance' from superclass 'UIViewController' with the same Objective-C selector

    【讨论】:

      【解决方案2】:

      将此添加到模态呈现的视图控制器中:

      override var modalPresentationCapturesStatusBarAppearance: Bool { 
          return true
      }
      

      【讨论】:

      • 我能够通过稍微调整您的解决方案来解决我的问题。似乎 modalPresentationCapturesStatusBarAppearance 不是一个可以被覆盖的属性,因为它是可变的。我的回答中提供了编译错误。
      【解决方案3】:
      1. 转到 info.plist 并将 UIViewControllerBasedStatusBarAppearance 设置为 false
      2. 转到AppDelegate.swift然后转到函数didFinishLaunchingWithOptions,设置你想要的颜色如下:

        UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent

      如果它可以帮助你,请告诉我。 谢谢。

      【讨论】:

      • 我只需要一个视图控制器来拥有灯光状态栏,所以为整个应用程序设置它并不理想。但是,我能够找到一个解决方案,这在我的回答中有所描述。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 2020-08-21
      • 2017-07-15
      • 2021-11-25
      相关资源
      最近更新 更多