设置 statusbarStyle(iOS 9.0 中已弃用)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50693665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:43:11  来源:igfitidea点击:

Setting statusbarStyle (deprecated in iOS 9.0)

iosswiftxcode

提问by Jacob Ahlberg

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

刚刚下载了新的 xCode 10.0 并看到旧的 statusBarStyle 自 iOS 9.0 以来已被弃用。

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

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

Deprecated code:UIApplication.shared.statusBarStyle = .default

弃用的代码:UIApplication.shared.statusBarStyle = .default

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

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



Edit

编辑

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

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

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

采纳答案by Paul King

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

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

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()

回答by Pavlos

Add View controller-based status bar appearance NOin Info.plist

添加View controller-based status bar appearance NOInfo.plist

And select Light in Status Bar Style in Deployment Info

并在部署信息中选择状态栏样式中的灯

enter image description here

在此处输入图片说明

回答by Md Rashed Pervez

In swift4, You can use this block of code below viewDidLoad()in your ViewController-

swift4 中,您可以viewDidLoad()在下面的代码块中使用ViewController-

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

回答by CryingHippo

If you use UINavigationController you also may want to use following code :

如果您使用 UINavigationController 您可能还想使用以下代码:

extension UINavigationController {
   open override var preferredStatusBarStyle: UIStatusBarStyle {
      return topViewController?.preferredStatusBarStyle ?? .default
   }
}

Reason is setNeedsStatusBarAppearanceUpdate()doesn't call childs preferredStatusBarStyle

原因是setNeedsStatusBarAppearanceUpdate()不叫孩子preferredStatusBarStyle

回答by cmilr

None of the other suggestions worked for me. I ended up getting it to work by:

其他建议都不适合我。我最终通过以下方式让它工作:

  1. Setting:

    override var preferredStatusBarStyle : UIStatusBarStyle {
        return .lightContent
    }
    
  2. Calling:

    setNeedsStatusBarAppearanceUpdate()
    
  1. 环境:

    override var preferredStatusBarStyle : UIStatusBarStyle {
        return .lightContent
    }
    
  2. 调用:

    setNeedsStatusBarAppearanceUpdate()
    

回答by hesham ghalaab

My solution was as this: making an extension from the navigation controller:

我的解决方案是这样的:从导航控制器进行扩展:

extension UINavigationController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        if let topViewController = presentedViewController{
            return topViewController.preferredStatusBarStyle
        }
        if let topViewController = viewControllers.last {
            return topViewController.preferredStatusBarStyle
        }

        return .default
    }
}

and if you have a viewController that will have another style than the style of the app , you can make this

如果你有一个视图控制器,它的风格不同于应用程序的风格,你可以做这个

var barStyle = UIStatusBarStyle.lightContent
override var preferredStatusBarStyle: UIStatusBarStyle{
    return barStyle
}

lets say that you app status style is .defaultand you want this screen to be .lightContentso barStyle will take the .lightContentas its default value, this will change the status bar style to lightContent, and then make sure when viewWillDisappearchange the barStyle again to the app status bar style which in our case is .default.

假设您的应用程序状态样式是.default并且您希望此屏幕.lightContent如此 barStyle 将.lightContent作为其默认值,这会将状态栏样式更改为 lightContent,然后确保viewWillDisappear再次将 barStyle 更改为应用程序状态栏样式在我们的例子中是.default.

this is works for me

这对我有用