0

I know there have been similar questions asked, but none of them have worked for me.

I have this code to enable swiping back in my project

    class InteractivePopRecognizer: NSObject {

    // MARK: - Properties

    fileprivate weak var navigationController: UINavigationController?

    // MARK: - Init

    init(controller: UINavigationController) {
        self.navigationController = controller

        super.init()

        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }
}

extension InteractivePopRecognizer: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return (navigationController?.viewControllers.count ?? 0) > 1
    }

    // This is necessary because without it, subviews of your top controller can cancel out your gesture recognizer on the edge.
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

I have this VC stack

HomescreenVC -> Login/SignupVC -> UserProfileVC

I do not want them to be able to swipe back from the UserProfileVC.

Luca Sarif
  • 142
  • 2
  • 13
  • try this link https://stackoverflow.com/questions/17209468/how-to-disable-back-swipe-gesture-in-uinavigationcontroller-on-ios-7 – Chirag Shah Aug 01 '18 at 12:42

2 Answers2

1

A better approach is to clear them from the stack when you show UserProfileVC

let profile  = self.storyboard?.instantiateViewController(withIdentifier: "profileID") as! UserProfileVC
self.navigationController?.viewControllers = [profile]

Edit: Do this inside profileVC

self.navigationController?.viewControllers = [self]

//

self.view.alpha = 0

UIView.animate(withDuration: 0.5) {

    self.view.alpha = 1

}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • I tried putting this in viewDidLoad and viewDidAppear of userprofilevc but that threw an error, and then i tried putting it after the segues to userprofilevc in the other VC's but that also threw an error. – Luca Sarif Aug 01 '18 at 11:12
  • remove any segue and give the profile an identifier inside IB , and load it like in answer – Shehata Gamal Aug 01 '18 at 11:15
  • this still doesnt work. I need segues to the view. Is there instead a way to dismiss all the view controllers below the current one (UserProfileVC)? – Luca Sarif Aug 01 '18 at 12:30
  • yes the edit does not work - I also forgot to mention that UserProfileVC is connected to a tabbar controller, sorry. – Luca Sarif Aug 01 '18 at 12:36
  • Thank you works well. Just before i accept, is there a way i could animate to the UserProfileVC instead of it just showing up. – Luca Sarif Aug 01 '18 at 13:03
  • you can't animate the view's frame as it will show black background , but you can animate alpha – Shehata Gamal Aug 01 '18 at 13:12
0

I think you can remove gesture Recognizer from there too why you are not trying that. Try something like this:-

view.gestureRecognizers?.removeAll()

  • this doesnt work i can still swipe back. i think its because im instantiating the gesture recognizer on the navigation controller instead of the actual view – Luca Sarif Aug 01 '18 at 12:07
  • yes it's due to that and checks if on the place of view by putting navigation. if not working check this http://holko.pl/ios/2014/04/06/interactive-pop-gesture/ – Ashutos Sahoo Aug 01 '18 at 12:15