当我点击设备像iphone中的照片时,如何隐藏状态栏和导航栏?
我用过
我用过
UIApplication.sharedApplication().setStatusBarHidden(false,withAnimation: UIStatusBarAnimation.Slide)
但它不工作。
编辑:我想隐藏和显示状态栏和导航栏,而不是永久隐藏它。
根据您的需要,您可以选择以下代码之一。
原文链接:https://www.f2er.com/swift/320592.html使用 – 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
.
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 } }