我用过
UIApplication.sharedApplication().setStatusbarHIDden(false,withAnimation: UIStatusBaranimation.SlIDe)
但它不工作。
编辑:我想隐藏和显示状态栏和导航栏,而不是永久隐藏它。
根据您的需要,您可以选择以下代码之一。使用 – setNavigationbarHIDden:animated:和一个UIbutton
以下代码显示如何使用故事板中的UIbutton设置并链接到@IBAction来切换状态栏和导航栏:
import UIKitclass 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 UIKitclass 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 UIKitclass 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 UIKitclass 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 }}总结
以上是内存溢出为你收集整理的swift – 如何在点击设备时隐藏状态栏和导航栏全部内容,希望文章能够帮你解决swift – 如何在点击设备时隐藏状态栏和导航栏所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)