【问题标题】:What is the correct way to manage statusBarStyle in Swift?在 Swift 中管理 statusBarStyle 的正确方法是什么?
【发布时间】:2020-03-03 06:57:35
【问题描述】:

我尝试在 info.plist 文件中将密钥 UIViewControllerBasedStatusBarAppearance 添加到 true ,然后在包含多个 UIViewController 类的 UINavigationController 类中添加以下代码。

class HomeNavigationController: UINavigationController {

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

但是,它不起作用。

我也尝试将navigationBarbarStyle 属性设置为.black,但这也不起作用。

还查看了https://stackoverflow.com/a/58203998/9180494,但这也无济于事。

请注意:对于未嵌入任何 UINavigationController 中的 UIViewController 类,如果我使用计算属性 preferredStatusBarStyle,那么它可以工作。

【问题讨论】:

    标签: swift statusbar ios13 swift5


    【解决方案1】:

    UINavigationController 类中尝试viewDidAppear()

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        navigationController?.navigationBar.barStyle = .black
    }
    

    同时添加(与上面相同的类):

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    

    【讨论】:

      【解决方案2】:

      @Anuranjan Bose 在你的视图上试试这个确实加载了,

      override func viewDidLoad() {
          super.viewDidLoad()
          setNeedsStatusBarAppearanceUpdate()
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-30
        • 2015-09-07
        • 1970-01-01
        • 2018-12-18
        • 1970-01-01
        • 1970-01-01
        • 2019-08-29
        • 2010-12-12
        相关资源
        最近更新 更多