iOS 9中不推荐使用setStatusBarHidden(_:withAnimation :)

时间:2015-09-27 13:47:55

标签: ios swift ios9 statusbar

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

5 个答案:

答案 0 :(得分:94)

请参阅preferredStatusBarUpdateAnimation

的Gif

enter image description here

代码

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

答案 1 :(得分:14)

Swift 3

  • 计算变量已取代某些功能
  • animate功能已更新语法
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
    }
}

答案 2 :(得分:11)

通过将更新移至didSet Swift 3 语法),我已经清理了 Leo的惊人的答案。

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

答案 3 :(得分:2)

如果您使用目标c编码,这是解决方案:)(Leo的目标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;}

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

答案 4 :(得分:0)

  • SWIFT 3 ALTERNATIVE
嘿伙计们,通过使用与每个覆盖的私有变量配对,为Swift 3找到了更简洁的方法。 我的原帖:https://stackoverflow.com/a/42083459/7183483

但这里是它的主旨:

这是一个片段:

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