2015-10-07 87 views
0

我試過在Swift中創建一個類,它在1秒後自動隱藏我的UIStatusBar和我的navigationController。 我的問題是,StatusBar不會消失。這就是我得到的:UIStatusBar不會消失

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) 

} 

回答

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 

希望這可以幫助你。

+0

所以我已經嘗試過,你可以在看到上面的示例是很重要的我例如.. 現在我刪除它,並嘗試第二個。這一個爲我工作,但不正確。此刻狀態欄是隱藏的,但導航控制器不會隱藏 –

+0

沒關係 - 它的工作原理:D謝謝 –

+0

嘿,如果它的工作,那麼請批准答案,這樣有助於其他 –

0

好吧..我解決了這樣的問題: 我創建了一個新的類HeaderAnimationHelper其中我創建了可用的方法。就像我可以從任何地方打電話一樣。

所以在這裏你可以看到輔助類:

進口的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!) 
    } 

} 

編輯15年10月7日:

我VE忘了提,它要依賴添加到Info.plist中

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

當心這種方法UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade) 是depricated