setStatusBarHidden(_:withAnimation:) 在 iOS 9 中弃用

setStatusBarHidden(_:withAnimation:) deprecated in iOS 9

我看到 iOS 9 中的 setStatusBarHidden(_:withAnimation:) 现在已被弃用,文档说使用 [UIViewController prefersStatusBarHidden] 代替,但如果我仍然使用 iOS 9 中的替代方案是什么想用幻灯片动画隐藏状态栏?

参考preferredStatusBarUpdateAnimation,

动图

代码

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
    }
 }

Swift 3

  • 计算变量取代了一些函数
  • 动画函数更新了语法

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
    }
}

我通过将更新移动到 didSet 来清理 Leo 的 惊人的答案 (Swift 3语法)。

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
    }
}

如果您使用 objective c 编码,这里是解决方案 :)(Leo 的 Objective C 版本 :P 谢谢大佬!!!)

声明一个变量

bool isHidden;
isHidden = false;//in viewDidload()

然后在你想隐藏状态栏的时候加上这段代码

isHidden = true;
[UIView animateWithDuration:0.6 animations:^{
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}];

之后添加这两个方法

-(UIStatusBarAnimation) preferredStatusBarUpdateAnimation
{
return UIStatusBarAnimationFade;
}

-(BOOL) prefersStatusBarHidden
{ return isHidden;}

希望你的问题得到解决(微笑)

  • SWIFT 3 备选方案

大家好,为 Swift 3 找到了一种更简洁的方法,即对每个覆盖使用私有变量配对。 我原来的post:

但重点是:

这是一个片段:

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
    }
}

然后我可以调用这样的函数:(这是我的示例之一,因此请忽略自定义函数)。

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()
        })
    })
}