setStatusBarHidden(_:withAnimation:) deprecated in iOS 9

后端 未结 5 1839
遥遥无期
遥遥无期 2020-12-13 01:39

I see that in iOS 9 setStatusBarHidden(_:withAnimation:) is now deprecated and the documentation says to use [UIViewController prefersStatusBarHidden]

相关标签:
5条回答
  • 2020-12-13 02:21

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-13 02:26

    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)

    0 讨论(0)
  • 2020-12-13 02:39
    • 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://stackoverflow.com/a/42083459/7183483

    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()
            })
        })
    }
    
    0 讨论(0)
  • 2020-12-13 02:42

    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
        }
     }
    
    0 讨论(0)
  • 2020-12-13 02:43

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题