简体   繁体   中英

How to disable back/left swipe gesture?

You can swipe from the left edge of the screen to go back on the navigation stack. But in my app, this behavior conflicts with my custom left menu. So, is it possible to disable this new gesture in UINavigationController?

From the controller you want this to be enabled/disabled just

Swift:

self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false // or true

ObjC:

self.navigationController.interactivePopGestureRecognizer.enabled = NO; // or YES

For my usecase it was important to disable it just in a single view controller, because that one had to have a slider which could easily interfere with that gesture. So I tried as suggested to disable it in viewWillAppear, which worked, however it kept being disabled in subsequently pushed controllers to.

I'v tried then to enable it in viewWillDisappear, which however did break the app somehow. Meaning, if in child view controllers I did try to use the back swipe the app was freezing. Putting it in background and reopen from there got rid of the freezing, but with nasty side-effects, like black backgrounds and missing search bars and other ui elements in wrong places.

The "trick" that got it working for me at the end was

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationController?.interactivePopGestureRecognizer?.delegate = nil
}

and without setting it back to the real delegate it did work so far

This feature set is not new as you are suggesting. If not wrong it was implemented in iOS 7 and can be found in all the subsequent releases. Its called interactivePopGestureRecognizer.

https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621847-interactivepopgesturerecognizer

Fortunately for us, Apple has provided of a way to disable it. Set it to false at a suitable place.

self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM