setStatusBarStyle:animated: deprecated

别说谁变了你拦得住时间么 提交于 2019-12-24 07:15:03

问题


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

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.

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?

Thanks in advance

EDIT:

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


回答1:


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
}



回答2:


In your info.plist, add the UIViewControllerBasedStatusBarAppearance key with a value of false.

Then, in your viewController when switching to your night theme:

UIApplication.shared.statusBarStyle = .lightContent

To go back to black:

UIApplication.shared.statusBarStyle = .default


来源:https://stackoverflow.com/questions/39434477/setstatusbarstyleanimated-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!