本文介绍了点击设备时如何隐藏状态栏和导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

当我在iPhone中像照片一样点击设备时,如何隐藏状态栏和导航栏?我用过

How to hide status bar and navigation bar when I tap the device like photos in iphone?I had used

UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Slide)

但是它不起作用.

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

Edit: I'd like to hide and show status bar and navigation bar, not permanently hide it.

推荐答案

使用Swift 5和iOS 12,您可以根据需要选择以下三个代码段之一来解决您的问题.

With Swift 5 and iOS 12, according to you needs, you may select one of the three following code snippets in order to solve your problem.

自iOS 8起,UINavigationController具有hidesBarsOnTap属性. hidesBarsOnTap具有以下声明:

Since iOS 8, UINavigationController has a hidesBarsOnTap property. hidesBarsOnTap has the following declaration:

var hidesBarsOnTap: Bool { get set }

Apple还声明了有关hidesBarsOnTap的信息:

Apple also states about hidesBarsOnTap:

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

The following code shows how to implement hidesBarsOnTap:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.hidesBarsOnTap = true
    }

    override var prefersStatusBarHidden: Bool {
        return navigationController?.isNavigationBarHidden == true
    }

    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return UIStatusBarAnimation.slide
    }

}


#2.使用UINavigationController setNavigationBarHidden(_:animated:)方法+ UIViewController prefersStatusBarHiddenpreferredStatusBarUpdateAnimation属性以及UIButton

UINavigationController具有称为setNavigationBarHidden(_:animated:)的方法. setNavigationBarHidden(_:animated:)具有以下声明:


#2. Using UINavigationController setNavigationBarHidden(_:animated:) method + UIViewController prefersStatusBarHidden and preferredStatusBarUpdateAnimation properties with a UIButton

UINavigationController has a method called setNavigationBarHidden(_:animated:). setNavigationBarHidden(_:animated:) has the following declaration:

func setNavigationBarHidden(_ hidden: Bool, animated: Bool)

以下代码显示了如何使用setNavigationBarHidden(_:animated:)并在情节提要中设置了UIButton并链接到@IBAction来切换状态栏和导航栏:

The following code shows how to toggle your status bar and navigation bar by using setNavigationBarHidden(_:animated:) with a UIButton set in the Storyboard and linked to a @IBAction:

import UIKit

class ViewController: UIViewController {

    // Link this @IBAction to a `UIButton`
    @IBAction func toggle(_ sender: UIButton) {
        navigationController?.setNavigationBarHidden(navigationController?.isNavigationBarHidden == false, animated: true)
    }

    override var prefersStatusBarHidden: Bool {
        return navigationController?.isNavigationBarHidden == true
    }

    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return UIStatusBarAnimation.slide
    }

}


#3.使用UINavigationController setNavigationBarHidden(_:animated:)方法+ UIViewController prefersStatusBarHiddenpreferredStatusBarUpdateAnimation属性以及UIGestureRecognizer

作为先前代码的替代方法,可以将setNavigationBarHidden(_:animated:)UIGestureRecognizer一起使用,而不是UIButton:


#3. Using UINavigationController setNavigationBarHidden(_:animated:) method + UIViewController prefersStatusBarHidden and preferredStatusBarUpdateAnimation properties with a UIGestureRecognizer

As an alternative to the previous code, you can use setNavigationBarHidden(_:animated:) with a UIGestureRecognizer instead of a UIButton:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let gesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.toggle))
        view.isUserInteractionEnabled = true
        view.addGestureRecognizer(gesture)
    }

    @objc func toggle() {
        navigationController?.setNavigationBarHidden(navigationController?.isNavigationBarHidden == false, animated: true)
    }

    override var prefersStatusBarHidden: Bool {
        return navigationController?.isNavigationBarHidden == true
    }

    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return UIStatusBarAnimation.slide
    }

}


  • 请确保在项目的 Info.plist 中将UIViewControllerBasedStatusBarAppearance设置为true,否则以前的示例代码将不起作用.
  • 如果需要定位到iOS 10,请参见此有关类似问题的答案.

    • Make sure that UIViewControllerBasedStatusBarAppearance is set to true in your project's Info.plist, otherwise the previous sample codes won't work.
    • See this answer for a similar question if you need to target iOS 10.
    • 这篇关于点击设备时如何隐藏状态栏和导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

      1403页,肝出来的..

09-06 19:23