vikrant tanwar
vikrant tanwar

Reputation: 219

not able to pop to viewcontroller more than once using uiswipegesturerecognizer default feature

Hello I am working on swift 4.0 and Xcode 10.2. I have added the following code to pop previous controller. If suppose I push three controller one by one then UISwipeGestureRecognizer can able to pop only once after that its not working.I am giving the following code details.I am stuck on this point and not getting any related answer.

self.navigationController?.interactivePopGestureRecognizer!.isEnabled = true
self.navigationController?.interactivePopGestureRecognizer!.delegate = self
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

Please give me solution.

Upvotes: 0

Views: 39

Answers (1)

PGDev
PGDev

Reputation: 24341

I've created the a similar hierarchy as yours with 3 controllers - VC1, VC2 and VC3

enter image description here

You can handle the interactivePopGestureRecognizer in controllers as follows:

  1. Set the interactivePopGestureRecognizer.delegate = self

  2. Implement gestureRecognizerShouldBegin(_:) method to return true/false to enable/diable the gesture.

Example:

In VC2: interactivePopGestureRecognizer is disabled

In VC3: interactivePopGestureRecognizer is enabled

class VC1: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

class VC2: UIViewController, UIGestureRecognizerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }

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

class VC3: UIViewController, UIGestureRecognizerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }

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

There is no need to manually set isEnabled property of interactivePopGestureRecognizer everytime in different controllers, i.e. remove the below code,

self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true

Upvotes: 1

Related Questions