在iphone中点击图片时如何隐藏状态栏和导航栏? 我用过
UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Slide)
但它不起作用。
编辑:我想隐藏和显示状态栏和导航栏,而不是永久隐藏。
最佳答案
对于 Swift 5 和 iOS 12,您可以根据需要选择以下三个代码片段之一来解决您的问题。
#1。使用 UINavigationController
hidesBarsOnTap
属性 + UIViewController
prefersStatusBarHidden
和 preferredStatusBarUpdateAnimation
属性
从 iOS 8 开始,UINavigationController
有一个 hidesBarsOnTap
属性。 hidesBarsOnTap
具有以下 declaration :
var hidesBarsOnTap: Bool { get set }
A Boolean value indicating whether the navigation controller allows hiding of its bars using a tap gesture.
Apple 还声明了关于 hidesBarsOnTap
的内容:
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 isfalse
.
下面的代码展示了如何实现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
prefersStatusBarHidden
和 preferredStatusBarUpdateAnimation
带有 UIButton
的属性
UINavigationController
有一个名为 setNavigationBarHidden(_:animated:)
的方法。 setNavigationBarHidden(_:animated:)
具有以下 declaration :
func setNavigationBarHidden(_ hidden: Bool, animated: Bool)
Sets whether the navigation bar is hidden.
下面的代码展示了如何使用 setNavigationBarHidden(_:animated:)
和 Storyboard 中设置的 UIButton
并链接到@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
prefersStatusBarHidden
和 preferredStatusBarUpdateAnimation
带有 UIGestureRecognizer
的属性
作为前面代码的替代方案,您可以使用 setNavigationBarHidden(_:animated:)
和 UIGestureRecognizer
而不是 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
}
}
- 确保
UIViewControllerBasedStatusBarAppearance
在项目的 Info.plist 中设置为true
,否则之前的示例代码将无法工作。 - 查看此 answer for a similar question如果您需要针对 iOS 10。
关于swift - 点击设备时如何隐藏状态栏和导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26273672/