在视图控制器之间更新状态栏样式 [英] Updating the status bar style between view controllers

查看:63
本文介绍了在视图控制器之间更新状态栏样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my info.plist, file I have View controller-based status bar appearance set to YES

我有一个FirstViewController,状态栏处于隐藏状态.

I have a FirstViewController where the status bar is hidden.

在我的SecondViewController中有

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

override var prefersStatusBarHidden: Bool {
    return false
}

override func viewDidLoad() {
    super.viewDidLoad()

    setNeedsStatusBarAppearanceUpdate()
}

但是,状态栏出现了,但是是黑色的.

However, the status bar is showing up, but is black.

如何使它正确更新?谢谢

How can I get it to update correctly? Thanks

AppDelegate.swift也有这个

UIApplication.shared.statusBarStyle = .lightContent

推荐答案

当视图控制器是导航控制器的子级时,如何管理状态栏样式存在许多误解.

There is a great deal of misunderstanding promulgated about how to govern the status bar style when your view controller is a child of a navigation controller.

您的子视图控制器可以实现preferredStatusBarStyle,如果导航栏为隐藏,则此控件将正确运行.

Your child view controllers can implement preferredStatusBarStyle, and this will work correctly if the navigation bar is hidden.

如果导航栏显示为 ,则导航控制器将基于导航栏barStyle的状态栏样式设置为.default;如果导航栏样式为.default,则将状态栏样式设置为.lightContent如果条形样式为.black.因此,在显示导航栏时,视图控制器设置状态栏样式的正确方法是设置导航控制器的导航栏样式.

If the navigation bar is showing, the navigation controller sets the status bar style based on the navigation bar's barStyle — to .default if the bar style is .default, and to .lightContent if the bar style is .black. So the correct way for your view controller to set the status bar style, when the navigation bar is showing, is to set the navigation controller's navigation bar style.

执行此操作的明显位置是viewWillAppear,只要此视图控制器成为导航控制器堆栈的顶部,就会调用它:

The obvious place to do this is in viewWillAppear, which is called whenever this view controller becomes the top of the navigation controller's stack:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.barStyle = .black // or .default
}

这篇关于在视图控制器之间更新状态栏样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆