**SWIFT 4.2**
Hey, I wanted to share a solution, that worked for me that I got from a great article on this ellusive subject by Graig Grummitt.
Step 1
As others have mentioned ADD below to your PLIST
View controller-based status bar appearance YES
Step 2 in the RootViewcontroller add below
var statusBarHidden: Bool = false {
didSet(newValue) {
UIView.animate(withDuration: 0.1) {
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
override var prefersStatusBarHidden: Bool {
return statusBarHidden
}
var vcStatusBarStyle: UIStatusBarStyle = .default {
didSet(newValue) {
UIView.animate(withDuration: 0.1) {
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return vcStatusbarStyle
}
When updating either property `statusBarHidden` or `vcStatusBarStyle` it will call `setNeedsStatusBarAppearanceUpdate()` and will update the status bar with the new values for either `prefersStatusBarHidden` or `preferredStatusBarStyle`. In my situation I had to update these properties for the container viewcontroller, that was the parent of the visable childviewcontroller. I did this using a simple delegate method.
protocol MainViewControllerDelegate {
func updateStatusBarStyle(statBarStayle: UIStatusBarStyle)
func toggleStatusBar(visable: Bool)
}
Ofcourse when instantiating the childViewController(Visible VC) don't forget to set the MainViewcontroller(Container VC) as its delegate. I sometimes do. :)
childViewController.delegate = self
Then in the childViewController I just called the delegate method when needed to update the status bar.
self.delegate?.updateStatusBarStyle(statBarStayle: .default)
As mentioned above Graig Grummitt goes into more detail about this solution and also working with UINavigationControllers as well. Link here: [The Mysterious Case of the Status Bar][1]
[1]:
[To see links please register here]