4

Here is my view controller: View Controller

I have no navigation bar but a back button which takes user back to previous view controller. What i want to implement is swipe to navigate back. I have searched a lot but did not find any help with proper description. Please help me implementing this.

Zohaib Ejaz
  • 387
  • 4
  • 18

3 Answers3

3

You can try this:

Add a gesture recognizer:

let swipeLeft = UISwipeGestureRecognizer()
swipeLeft.addTarget(self, action: #selector(backSegue) )
swipeLeft.direction = .left
self.view!.addGestureRecognizer(swipeLeft)

@IBAction func backSegue() {
    performSegue(withIdentifier: "backSegue", sender: self)
}

Add this in the ViewController you want to go back to:

@IBAction func unwindBack(segue:UIStoryboardSegue) {
    print("Welcome back!!!")
}

on the ViewController were you are adding the swipe recognizer make a manual segue from backSegue() IBAction to the exit icon on top. Select the ùnwindBack()` outlet and name the segue 'backSegue'. See this tutorial if you need more details.

pesch
  • 1,976
  • 2
  • 17
  • 29
  • I haven't been able to test it, let me know if you run into trouble and I'll try it in Xcode – pesch May 25 '17 at 10:44
  • `UISwipeGestureRecognizer` is part of `UIKit` and you are adding it programmatically to your view in this line: `self.view!.addGestureRecognizer(swipeLeft)` – pesch May 25 '17 at 11:25
  • Thanks a lot @pesch. It worked. Can you please give me some tip making the navigation to previous view controller animated? –  May 25 '17 at 11:37
  • The easy way will be to select the unwind segue and check Animates in the Attributes Inspector. However, if you want something more sophisticated, you need to use `UIViewControllerAnimatedTransitioning` – pesch May 25 '17 at 13:38
0

Try using touches moved, begin and ended delegate methods to find ur touch movement. Then based on the deviation in x or y position you can navigate the user.

jegadeesh
  • 875
  • 9
  • 22
0

You can do one thing.

1.Use Navigation Controller for which keep your first view controller as root ViewController.

2.On tap or your any action user will navigate to second view controller , which will have navigation bar.

3.In your viewDidAppear() class make hidden true of navigation bar

self.navigationController!.isNavigationBarHidden=false

4.Add custom button for back.

5.This procedure will make you navigate back on swipe & navigation bar will also be not there.

6.Also on click on your custom back button add code for

_ = self.navigationController?.popViewController(animated: true)

This will be easy way for your customization.

Kalyani
  • 392
  • 2
  • 17