setStatusBarStyle:animated: 不推荐使用 [英] setStatusBarStyle:animated: deprecated

查看:34
本文介绍了setStatusBarStyle:animated: 不推荐使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 Xcode 8 beta 上开发一个应用程序(在 Swift 3 中),用于 iOS 10.

I'm currently developing an app (in Swift 3) on Xcode 8 beta, for iOS 10.

我想要实现的是在运行时更改视图控制器中的状态栏样式,以便将主题从白天主题更改为夜间主题.我发现我过去在开发另一个应用程序时使用的方法已被弃用,如图 此处 API 参考.

What I want to achieve is to change status bar style within a view controller at run time, for changing the theme from daytime theme to night theme. I've found out that the method I used to use when I was developing another app in the past was deprecated, as shown here on the API reference.

但是,preferredStatusBarStyle 在这里不起作用,因为我想在单个视图控制器中更改它.

However, preferredStatusBarStyle won't work here since I would like to change it within a single view controller.

谁能想到其他方法来执行此操作?

Can anybody think of other ways to perform this?

提前致谢

明确地说,我想要做的是在视图控制器已经在屏幕上时更改样式.

To be clear, what I want to do is to change the style when the view controller is already on screen.

推荐答案

您可以创建一个 statusBarStyle 变量,该变量在更改时更新状态栏外观.如果您只希望此效果影响一个控制器,只需在控制器消失或确实消失时反转效果即可.

You can create a statusBarStyle variable that when changed updates the status bar appearance. If you only want this to affect one controller, simply reverse the effect when the Controller will or did disappear.

var statusBarStyle: UIStatusBarStyle = .lightContent {
    didSet {
        setNeedsStatusBarAppearanceUpdate()
    }
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return statusBarStyle
}

上述解决方案会在控制器出现之前覆盖之前控制器的状态栏样式.如果你想在控制器出现时改变状态栏样式,试试这个:

The above solution will override the previous controller's status bar style before the controller appears. If you want to change the status bar style when the controller appears, try this:

var statusBarStyle: UIStatusBarStyle? {
    didSet {
        setNeedsStatusBarAppearanceUpdate()
    }
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return statusBarStyle ?? super.preferredStatusBarStyle
}

override func viewDidAppear(animated: Bool) {

    super.viewDidAppear(animated)

    statusBarStyle = .lightContent
}

这篇关于setStatusBarStyle:animated: 不推荐使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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