【问题标题】:Hidden Statusbar in a function在函数中隐藏状态栏
【发布时间】:2016-03-24 12:38:28
【问题描述】:

当我隐藏导航栏和工具栏时,我想隐藏状态栏。

我怎样才能使状态栏隐藏在if NavigationBar.hidden == false && Toolbar.hidden == false{} 中? 我不知道如何做到这一点,我知道返回 Statusbarhidden 的函数,但那是在整个 ViewController 中,我想将它隐藏在函数中。

感谢您的帮助。

  func ImageTapGesture () {
    if NavigationBar.hidden == false && Toolbar.hidden == false{
        NavigationBar.hidden = true
        Toolbar.hidden = true

    } else if NavigationBar.hidden == true && Toolbar.hidden == true  {
        NavigationBar.hidden = false
        Toolbar.hidden = false

    }
}

【问题讨论】:

标签: swift hidden statusbar func


【解决方案1】:

在Swift语言下,可以参考下面隐藏的代码,不需要动画效果可以注释掉。

    var isHidden:Bool = false

    @IBAction func clicked(_ sender: AnyObject) {
        isHidden = !isHidden
        UIView.animate(withDuration: 0.5, animations: { () -> Void in
            self.setNeedsStatusBarAppearanceUpdate()
        }) 
    }
    override var preferredStatusBarUpdateAnimation : UIStatusBarAnimation {
        return UIStatusBarAnimation.slide
    }
    override var prefersStatusBarHidden : Bool {
        return isHidden
    }

也可以参考下面链接的Demo,是我写的项目需求。

Github:https://github.com/ReverseScale/HiddenStatusBar

希望我能帮助你。

【讨论】:

    【解决方案2】:

    一个 Swift 2.x 兼容的解决方法,让你需要做什么:

    func hideStatusBar(yOffset:CGFloat) { // -20.0 for example
        let statusBarWindow = UIApplication.sharedApplication().valueForKey("statusBarWindow") as! UIWindow
        statusBarWindow.frame = CGRectMake(0, yOffset, statusBarWindow.frame.size.width, statusBarWindow.frame.size.height)
    }
    
    func showStatusBar() {
        let statusBarWindow = UIApplication.sharedApplication().valueForKey("statusBarWindow") as! UIWindow
        statusBarWindow.frame = CGRectMake(0, 0, statusBarWindow.frame.size.width, statusBarWindow.frame.size.height)
    }
    

    以使用它为例,您可以启动:

    hideStatusBar(-20.0)
    

    【讨论】:

    • 感谢您的帮助。
    猜你喜欢
    • 2011-04-29
    • 2015-11-11
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多