UIStatusBar不会消失

时间:2015-10-07 09:46:05

标签: swift uinavigationcontroller swift2 uistatusbar

我尝试在Swift中创建一个类,它在1秒后自动隐藏我的UIStatusBarnavigationController。 我的问题是,StatusBar不会消失。这就是我得到的:

override func viewDidLoad() {
    super.viewDidLoad()
    NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "prefersStatusBarHidden", userInfo: nil, repeats: false)
}
override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
    }

override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
    return UIStatusBarAnimation.Fade
}

override func prefersStatusBarHidden() -> Bool {
    if (barcounter == 0){
        hide()
        barcounter = 1
        return true
    }
    else {
        show()
        barcounter = 0
        return false
    }
}

@IBAction func picturePressed(sender: AnyObject) {
    prefersStatusBarHidden()
}

func hide(){

    UIView.animateWithDuration(1, delay: 1, options: UIViewAnimationOptions.CurveEaseOut, animations: {

        self.navigationController?.navigationBar.alpha = 0.0

        }, completion: nil)

}

func show(){
    UIView.animateWithDuration(1, delay: 1, options: UIViewAnimationOptions.CurveEaseOut, animations: {

        self.navigationController?.navigationBar.alpha = 1.0

        }, completion: nil)

}

2 个答案:

答案 0 :(得分:1)

您需要在任何想要隐藏uistatusbar的视图控制器中覆盖此方法。

override func prefersStatusBarHidden() -> Bool {
    return true;
}

如果它不起作用,那么试试这个: -

In Info.plist set View controller-based status bar appearance to NO

And call UIApplication.sharedApplication().statusBarHidden = true

希望这会对你有所帮助。

答案 1 :(得分:0)

好吧..我这样解决了: 我创建了一个新类HeaderAnimationHelper,在其中创建了可用的方法。就像我可以从任何地方都这样称呼它。

所以在这里你可以看到Helper类:

导入UIKit

class HeaderAnimationHelper {

    static let sharedInstance = HeaderAnimationHelper()
    var navi: UINavigationController!

    func hideController(var barcounter: Int, navigationController: UINavigationController) -> Int {
        navi = navigationController
        if (barcounter == 0){
            barcounter = 1
            UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.Fade)
            hide()
        }
        else {
            show()
            barcounter = 0
            UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
        }
        return barcounter
    }

    func hide(){

        UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {

            self.navi.navigationBar.alpha = 0.0

            }, completion: nil)

    }

    func show(){
        UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {

            self.navi.navigationBar.alpha = 1.0

            }, completion: nil)

    }


}

并且下一个类是主要类,您可以在其中放置所有代码和内容...... 我是这样创建的:

import UIKit

class ContactMeViewController: UIViewController {


    var barcounter = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "animate", userInfo: nil, repeats: false)
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
    }


    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
        return UIStatusBarAnimation.Fade
    }

    @IBAction func picturePressed(sender: AnyObject) {
        animate()
    }

    func animate(){
        barcounter = HeaderAnimationHelper.sharedInstance.hideController(barcounter, navigationController: self.navigationController!)
    }

}

编辑10/07/15:

我忘了提及,将依赖项添加到Info.plist中非常重要

In Info.plist set View controller-based status bar appearance to NO

注意这种方法UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)        被诽谤