Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setStatusBarHidden(_:withAnimation:) deprecated in iOS 9

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?

like image 302
jacobsieradzki Avatar asked Sep 27 '15 13:09

jacobsieradzki


4 Answers

Refer to preferredStatusBarUpdateAnimation,

Gif

enter image description here

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
    }
 }
like image 132
Leo Avatar answered Nov 06 '22 09:11

Leo


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
    }
}
like image 23
Tim Windsor Brown Avatar answered Nov 06 '22 10:11

Tim Windsor Brown


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
    }
}
like image 13
Alex Staravoitau Avatar answered Nov 06 '22 10:11

Alex Staravoitau


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)

like image 4
zahid hasan Avatar answered Nov 06 '22 10:11

zahid hasan