UIStatusBar не исчезнет

Я пытался создать класс в Swift, который автоматически скрывает мой UIStatusBar и мой navigationController через 1 секунду. Моя проблема в том, что 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 ответа

Решение

Вам нужно переопределить этот метод в любом контроллере представления, в котором вы хотите скрыть 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

надеюсь, это поможет вам.

Хорошо.. Я решил это так: я создал новый класс 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) ограничен

Другие вопросы по тегам