首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIStatusBar不会消失

UIStatusBar不会消失
EN

Stack Overflow用户
提问于 2015-10-07 09:46:04
回答 2查看 186关注 0票数 0

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

代码语言:javascript
复制
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)

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-07 09:57:47

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

代码语言:javascript
复制
override func prefersStatusBarHidden() -> Bool {
    return true;
}

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

代码语言:javascript
复制
In Info.plist set View controller-based status bar appearance to NO

And call UIApplication.sharedApplication().statusBarHidden = true

希望这能帮到你。

票数 1
EN

Stack Overflow用户

发布于 2015-10-07 15:01:04

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

在这里,您可以看到Helper类:

导入UIKit

代码语言:javascript
复制
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)

    }


}

下一堂课是你可以把你所有的代码放在其中的主类.我是这样创造的:

代码语言:javascript
复制
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中是很重要的

代码语言:javascript
复制
In Info.plist set View controller-based status bar appearance to NO

注意这个方法-- UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)被削弱了

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32989015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档