本文介绍了设置statusbarStyle(在iOS 9.0中已弃用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅下载了新的xCode 10.0,并发现自iOS 9.0起不赞成使用旧的statusBarStyle.

Just downloaded the new xCode 10.0 and saw that the old statusBarStyle has been deprecated since iOS 9.0.

警告: Setter for 'statusBarStyle' was deprecated in iOS 9.0: Use -[UIViewController preferredStatusBarStyle]

不推荐使用的代码:UIApplication.shared.statusBarStyle = .default

我尝试使用self.preferredStatusBarStyle,但发现该属性只是一个吸气剂.所以有人知道如何设置statusBarStyle?

I tried using self.preferredStatusBarStyle, but found out the property is only a getter. So anyone knows how to set the statusBarStyle?

我想更改一个函数内的statusBarStyle,用户可以在其中切换不同的主题.例如:

I want to change the statusBarStyle inside a function, where a user can switch between different themes. For example:

func changeStatusBar(toDarkMode: Bool) {
    if toDarkMode {
        // Set to light statusBarStyle
    } else {
        // Set to default
    }
}

推荐答案

使用与现在相同的代码设置darkMode变量,然后在系统期望的计算变量中使用它:

Set your darkMode variable using the same code you have now, then use it in the computed variable that the system is expecting:

var darkMode = false
override var preferredStatusBarStyle : UIStatusBarStyle {
    return darkMode ? .default : .lightContent
}

根据上下文,您可能需要强制刷新屏幕以使其生效.您可以通过以下调用来做到这一点:

Depending on the context you may need to force a refresh of the screen for it to take effect. You would do that with the following call:

setNeedsStatusBarAppearanceUpdate()

这篇关于设置statusbarStyle(在iOS 9.0中已弃用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 13:30