I see that in iOS 9 setStatusBarHidden(_:withAnimation:)
is now deprecated and the documentation says to use [UIViewController prefersStatusBarHidden]
instead but what is the alternative in iOS 9 if I still want to hide the status bar with a slide animation?
Refer to preferredStatusBarUpdateAnimation
,
Gif
Code
class ViewController: UIViewController {
var isHidden:Bool = false{
didSet{
UIView.animate(withDuration: 0.5) { () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
@IBAction func clicked(sender: AnyObject) {
isHidden = !isHidden
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
return .slide
}
override var prefersStatusBarHidden: Bool{
return isHidden
}
}
View controller-based status bar appearance
= NO, this might help: https://mcmap.net/q/67647/-hide-the-status-bar-in-ios-9 –
Toddy Swift 3
- Computed variables have replaced some functions
- The animate function has updated syntax
class ViewController: UIViewController {
var isHidden:Bool = false
@IBAction func clicked(sender: AnyObject) {
isHidden = !isHidden
UIView.animate(withDuration: 0.5) { () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
}
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return UIStatusBarAnimation.slide
}
override var prefersStatusBarHidden: Bool {
return isHidden
}
}
I have cleaned up Leo's amazing answer a bit by moving the update to didSet
(Swift 3 syntax).
class ViewController: UIViewController {
@IBAction func clicked(sender: AnyObject) {
statusBarHidden = !statusBarHidden
}
var statusBarHidden = false {
didSet {
UIView.animate(withDuration: 0.5) { () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
override var prefersStatusBarHidden: Bool {
return statusBarHidden
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
}
if you are coding with objective c, Here is the solution :)(Leo's Objective C version :P thanks man!!!)
declare a variable
bool isHidden;
isHidden = false;//in viewDidload()
and then add this code when you want to hide status bar
isHidden = true;
[UIView animateWithDuration:0.6 animations:^{
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}];
after that add this two method
-(UIStatusBarAnimation) preferredStatusBarUpdateAnimation
{
return UIStatusBarAnimationFade;
}
-(BOOL) prefersStatusBarHidden
{ return isHidden;}
Hope your problem will be solve (smile)
- SWIFT 3 ALTERNATIVE
Hey guys, found a much neater way of going about it for Swift 3, by using a private var pairing with each of the overrides. My original post: https://mcmap.net/q/67808/-preferredstatusbarupdateanimation-being-ignored
but here's the jist of it:
Here's a snippet:
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
get {
return .slide
}
}
private var statusBarStyle : UIStatusBarStyle = .default
override var preferredStatusBarStyle: UIStatusBarStyle {
get {
return statusBarStyle
}
}
private var statusBarStatus : Bool = false
override var prefersStatusBarHidden: Bool {
get {
return statusBarStatus
}
}
which I then could call in a function like so: (this is one of my examples, so disregard the custom function).
func sliderView(sliderView: SliderView, didSlideToPlace: CGFloat, within: CGFloat) {
let val = (within - (didSlideToPlace - sliderView.upCent))/(within)
print(val)
//Where you would change the private variable for the color, for example.
if val > 0.5 {
statusBarStyle = .lightContent
} else {
statusBarStyle = .default
}
UIView.animate(withDuration: 0.5, animations: {
sliderView.top.backgroundColor = UIColor.black.withAlphaComponent(val)
self.coverLayer.alpha = val
self.scroll.backgroundColor = colors.lightBlueMainColor.withAlphaComponent(val)
}, completion: {
value in
//If you do not call setNeedsStatusBarAppearanceUpdate() in an animation block, the animation variable won't be called it seems.
UIView.animate(withDuration: 0.4, animations: {
self.animating = true
//Where you set the status for the bar (your part of the solution)
self.statusBarStatus = false
//Then you call for the refresh
self.setNeedsStatusBarAppearanceUpdate()
})
})
}
© 2022 - 2024 — McMap. All rights reserved.