Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 260
Digvijay Gida Avatar asked Sep 15 '25 22:09

Digvijay Gida


2 Answers

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
like image 116
Durdu Avatar answered Sep 19 '25 22:09

Durdu


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

like image 44
Hons Avatar answered Sep 19 '25 22:09

Hons