Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to disable and enable swipe back for navigation controllers in tab bar controllers

I have a TabBarController with three tabs. The first and last tabs are navigation controllers that I want to have swipe back enabled in. The middle tab is just a standard viewController. By default it seems like swipe back is not enabled. I guess that is because I'm using a back button in these two navigationControllers. I have tried the following code in the root view controllers of the two navigation controllers to enable it:

In viewDidLoad :

self.navigationController?.interactivePopGestureRecognizer?.delegate = self

and conforming to the UIGestureRecognizerDelegate:

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {

        if (self.navigationController?.viewControllers.count)! > 1 {
            return true
        }

        return false
    }

The swipe back to work now works however occasionally if one of the view controllers pushes another on top the UI and app will all freeze up. What is the correct way to enable and disable swipe back in Swift?

like image 406
Kex Avatar asked Oct 08 '16 14:10

Kex


People also ask

How do I turn off swipe back?

As a side note, to quickly return to default settings without using the ADB command, go to Settings –> System –> Gestures –> System navigation on your device. Tap the gear icon, then adjust the gesture sensitivity slider for the left edge to set things back to normal.

How do I turn off swipe gestures in iOS?

Answer: A: As stated in this article, Switch between apps on iPad - Apple Support. To turn off the multifinger swipe gesture, go to Settings > Home Screen & Dock > Multitasking.

How do I add a tab bar to my navigation controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

How does tab bar controller work?

When the user selects a specific tab, the tab bar controller displays the root view of the corresponding view controller, replacing any previous views. (User taps always display the root view of the tab, regardless of which tab was previously selected. This is true even if the tab was already selected.)


2 Answers

For people like me who find this 2 years later without an answer

ViewController

In viewDidAppear :

self.navigationController?.interactivePopGestureRecognizer?.delegate = self

and your UIGestureRecognizerDelegate

extension YourViewController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    if gestureRecognizer.isEqual(navigationController?.interactivePopGestureRecognizer) {
        navigationController?.popViewController(animated: true)
    }
    return false
}
like image 65
A V L Avatar answered Sep 19 '22 12:09

A V L


Two steps to achieve this:

  1. Add the UIGestureRecognizerDelegate to your class inheritance
  2. In viewDidLoad add the following:

To enable swipe back:

navigationController?.interactivePopGestureRecognizer?.isEnabled = true

To disable swipe back:

navigationController?.interactivePopGestureRecognizer?.isEnabled = false
like image 30
Rashwan L Avatar answered Sep 19 '22 12:09

Rashwan L