I'm working on an iPhone app with XCode 10 and iOS 11.
I'm trying to have the status bar animate out of the screen when moving from one scene to another.
In my first view controller (TableViewController) I declare:
var isStatusBarHidden = false
I then add the following:
override var prefersStatusBarHidden: Bool {
return isStatusBarHidden
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
and then in the prepare method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "resultSegue" {
...
isStatusBarHidden = true
UIView.animate(withDuration: 0.5, animations: {
self.setNeedsStatusBarAppearanceUpdate()
})
}
}
In the second view controller (ViewController) I have
var isStatusBarHidden = true
and
override var prefersStatusBarHidden: Bool {
return true
}
The animation works fine on the iPhone 8. However, on the iPhone X, even though the status bar does appear and disappear, it doesn't animate.
How can I have the animation happen on iPhone X too?
I got your code to work nicely, as long as I did not have the first view controller embedded in an UINavigationController.
If you have your first view controller in an UINavigationController: Make a subclass, f.ex CustomNavigationController:
class CustomNavigationController: UINavigationController {
var isStatusBarHidden: Bool = false
override var prefersStatusBarHidden: Bool {
return isStatusBarHidden
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
}
Then in the prepare for segue method in the first view controller:
if let navigationController = navigationController as? CustomNavigationController {
navigationController.isStatusBarHidden = true
UIView.animate(withDuration: 0.5, animations: {
self.setNeedsStatusBarAppearanceUpdate()
})
}
And as you have in your second view controller:
override var prefersStatusBarHidden: Bool {
return true
}
Then it animated nicely for me on the iPhone X.