2015-10-07 59 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