setStatusBarHidden(_:withAnimation:) deprecated in

2019-01-21 11:02发布

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?

5条回答
一纸荒年 Trace。
2楼-- · 2019-01-21 11:11
  • 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()
        })
    })
}
查看更多
爷、活的狠高调
3楼-- · 2019-01-21 11:21

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
    }
}
查看更多
在下西门庆
4楼-- · 2019-01-21 11:24

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)

查看更多
贪生不怕死
5楼-- · 2019-01-21 11:26

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
    }
}
查看更多
我只想做你的唯一
6楼-- · 2019-01-21 11:30

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
    }
 }
查看更多
登录 后发表回答