隐藏导航栏时如何启用滑动手势?
How to enable swipe gesture when navigation bar is hidden?
我已经尝试解决这个问题很长一段时间了,但一直想不通。我有当前设置:
在每个视图控制器中,我像这样隐藏导航栏:
self.navigationController?.setNavigationBarHidden(true, animated: true)
问题是我在隐藏导航栏的视图控制器上松开了滑动手势。我需要启用动画但无法使用:
self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true
任何帮助都会很棒,因为我相信很多人 运行 都对这个问题感兴趣。谢谢!
答案如下:只需将 NavigationController 子类化并执行以下操作。
import UIKit
class YourUINavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
}
extension YourUINavigationController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}
您可以通过以下方式处理滑动手势,这将帮助您避免应用冻结。
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animate
{
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
if (self.navigationController.viewControllers.count > 1)
{
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
else
{
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
}
我已经尝试解决这个问题很长一段时间了,但一直想不通。我有当前设置:
在每个视图控制器中,我像这样隐藏导航栏:
self.navigationController?.setNavigationBarHidden(true, animated: true)
问题是我在隐藏导航栏的视图控制器上松开了滑动手势。我需要启用动画但无法使用:
self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true
任何帮助都会很棒,因为我相信很多人 运行 都对这个问题感兴趣。谢谢!
答案如下:只需将 NavigationController 子类化并执行以下操作。
import UIKit
class YourUINavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
}
extension YourUINavigationController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}
您可以通过以下方式处理滑动手势,这将帮助您避免应用冻结。
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animate
{
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
if (self.navigationController.viewControllers.count > 1)
{
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
else
{
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
}