加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 如何在点击设备时隐藏状态栏和导航栏

发布时间:2020-12-14 05:55:44 所属栏目:百科 来源:网络整理
导读:当我点击设备像iphone中的照片时,如何隐藏状态栏和导航栏? 我用过 UIApplication.sharedApplication().setStatusBarHidden(false,withAnimation: UIStatusBarAnimation.Slide) 但它不工作。 编辑:我想隐藏和显示状态栏和导航栏,而不是永久隐藏它。 根据
当我点击设备像iphone中的照片时,如何隐藏状态栏和导航栏?
我用过
UIApplication.sharedApplication().setStatusBarHidden(false,withAnimation: UIStatusBarAnimation.Slide)

但它不工作。

编辑:我想隐藏和显示状态栏和导航栏,而不是永久隐藏它。

根据您的需要,您可以选择以下代码之一。

使用 – setNavigationBarHidden:animated:和一个UIButton

以下代码显示如何使用故事板中的UIButton设置并链接到@IBAction来切换状态栏和导航栏:

import UIKit

class ViewController: UIViewController {

    // Link this @IBAction to a `UIButton` in your Storyboard
    @IBAction func toggle(sender: AnyObject) {
        navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false,animated: true)
    }

    override func prefersStatusBarHidden() -> Bool {
        return navigationController?.navigationBarHidden == true
    }

    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
        return UIStatusBarAnimation. Slide
    }

}

使用 – setNavigationBarHidden:animated:和一个UIGestureRecognizer

作为以前代码的替代方法,您可以使用UIGestureRecognizer而不是UIButton:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let gesture = UITapGestureRecognizer(target: self,action: "toggle:")
        view.userInteractionEnabled = true
        view.addGestureRecognizer(gesture)
    }

    func toggle(sender: AnyObject) {
        navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false,animated: true)
    }

    override func prefersStatusBarHidden() -> Bool {
        return navigationController?.navigationBarHidden == true
    }

    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
        return UIStatusBarAnimation.Slide
    }

}

使用hidesBarsOnTap

自iOS 8以来,UINavigationController具有一个hidesBarsOnTap属性(declaration)。苹果公司说:

When the value of this property is true,the navigation controller toggles the hiding and showing of its navigation bar and toolbar in response to an otherwise unhandled tap in the content area. The default value of this property is false.

以下代码显示了如何实现hidesBarsOnTap:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.hidesBarsOnTap = true
    }

    override func prefersStatusBarHidden() -> Bool {
        return navigationController?.navigationBarHidden == true
    }

    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
        return UIStatusBarAnimation.Slide
    }

}

请注意,如果您已经在UINavigationController中添加了一个,则以前的代码也将切换您的导航工具栏。

使用navigationBarHidden和animateWithDuration:动画:

如果要控制动画持续时间,可以使用以下代码:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let button = UIButton(type: UIButtonType.System)
        button.setTitle("Button",forState: UIControlState.Normal)
        button.addTarget(self,action: "toggle:",forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(button)

        // Auto layout code using anchors (requires iOS 9)
        button.translatesAutoresizingMaskIntoConstraints = false
        let horizontalConstraint = button.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor)
        let verticalConstraint = button.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor)
        NSLayoutConstraint.activateConstraints([horizontalConstraint,verticalConstraint])
    }

    func toggle(sender: AnyObject) {
        UIView.animateWithDuration(2) {
            self.navigationController?.navigationBarHidden = self.navigationController?.navigationBarHidden == false
            //self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false,animated: false) // also works
        }
    }

    override func prefersStatusBarHidden() -> Bool {
        return navigationController?.navigationBarHidden == true
    }

    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
        return UIStatusBarAnimation.Slide
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读