How to enable swipe gesture when navigation bar is

2019-03-03 20:29发布

I've been trying to solve this issue for quite some time and cant figure it out. I have the current set up:

enter image description here

In each view controller I hide the navigation bar like so:

        self.navigationController?.setNavigationBarHidden(true, animated: true)

The issue is I loose the swipe gesture on the view controllers that the navigation bar is hidden. I need to have the animation enabled and cannot use:

    self.navigationController?.navigationBar.isHidden = true
    self.navigationController?.isNavigationBarHidden = true 

Any help would be awesome as I'm sure many people has ran into this issue. Thanks!

2条回答
我只想做你的唯一
2楼-- · 2019-03-03 20:39

You can handle swipe gesture by doing the following, and its will help you to avoid freezing the app.

- (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;
        }
    }
}
查看更多
等我变得足够好
3楼-- · 2019-03-03 20:50

Here is the answer: Just subclass your NavigationController and do the following.

  import UIKit

class YourUINavigationController: UINavigationController {
        override func viewDidLoad() {
            super.viewDidLoad()
            interactivePopGestureRecognizer?.delegate = self
        }
    }

    extension VaultUINavigationController: UIGestureRecognizerDelegate {
        func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
            return viewControllers.count > 1
        }
    }
查看更多
登录 后发表回答