1

I have a view controller where I need to add a custom back button. So I have added a custom backButtonItem. But After adding custom back button the default behavior of my view controller to go back by swipe stops working as expected.

If I remove the custom back button from the view controller, the behavior of view controller is as expected but as soon as I add custom back button the default behavior stops.

I have added the custom back button like this

self.navigationItem.leftBarButtonItem = getCustomBackBarButtonItem(viewController: self)

I have tried to use backBarButtonItem instead of leftBarButtonItem, but by doing that the custom back button doesn't appear and the view controller's behavior is as expected.

If I remove the above code the behavior of the view controller is as expected and it smoothly goes back by swipe.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Akruti
  • 183
  • 1
  • 12
  • Please take a look at [this](https://stackoverflow.com/questions/27713747/execute-action-when-back-bar-button-of-uinavigationcontroller-is-pressed) & [this](https://stackoverflow.com/questions/19054625/changing-back-button-in-ios-7-disables-swipe-to-navigate-back/20330647#20330647) – Lal Krishna Dec 12 '17 at 06:23

3 Answers3

1

Be sure to build the UIBarButtonItem doing something like this:

let customBack = UIBarButtonItem(title: "Back", style:.done, target:self, action: #selector(self.letsGoBack))

and then implement the pop back function with:

@objc func letsGoBack() {
   self.navigationController?.popViewController(animated: true)
}

so at the end it's just:

self.navigationItem.leftBarButtonItem = customBack

if you want to keep the swipe back gesture, you might subclass your navigation controller doing so:

class YourNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.interactivePopGestureRecognizer?.delegate = self
    }

    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return self.viewControllers.count > 1
    }
}
mugx
  • 9,869
  • 3
  • 43
  • 55
  • By using "self.navigationItem.leftBarButtonItem = getCustomBackBarButtonItem(viewController: self)" code, the behavior of the back button is as expected. I can move to previous view controller successfully by pressing the back button, but the default behavior(by swiping left to the right user can go back to previous view controller of navigation stack) of navigation controller stops. – Akruti Dec 12 '17 at 06:45
  • I added you the explanation for `interactivePopGestureRecognizer`, so you can re-enable the swipe back – mugx Dec 12 '17 at 06:56
  • @andrew, okay. I will try this and let you know. But I just wanted to know that why the default behavior of navigation controller stops after adding the custom back button? – Akruti Dec 12 '17 at 06:59
  • you may check here: https://stackoverflow.com/questions/23766659/custom-back-button-title-and-keep-the-swipe-back-gesture or here: http://www.pixeldock.com/blog/enable-the-swipe-back-gesture-aka-interactive-pop-gesture-when-using-a-uinavigationcontroller-with-custom-back-button/ – mugx Dec 12 '17 at 07:01
  • Ok @andrew. Thanks – Akruti Dec 12 '17 at 07:25
  • @andrew, yes. Thanks – Akruti Dec 12 '17 at 07:29
1

Instead of setting delegate to self you can simply add this line:

self.navigationController?.interactivePopGestureRecognizer?.delegate = nil

So it won't go to any of interactive PopGestureRecognizer's delegate method and the navigation Controller behavior will be as per your expectation. This is the small workaround to achieve the expected behavior.

Patrick R
  • 6,621
  • 1
  • 24
  • 27
0

To navigate back to previous viewController

Initialize UIBarButton

var backBtn:UIBarButtonItem!

add UIBarButton to NavigationBar

backBtn = UIBarButtonItem(title: "Go-Back" , style: .plain, target: self, action: #selector(self.backBtnClicked(_:)))

self.navigationItem.leftBarButtonItem = backBtn

add this function into your class

func backBtnClicked(_ sender:UIBarButtonItem) {
   if let redirect = self.navigationController?.popViewController(animated: true) {
    // If you open this viewController by using pushViewController this will called
   } else {
      self.dismiss(animated: true, completion: nil) 
      // If you open this viewController by using present this will called
   }
}

Hope this will help you

Ganesh Manickam
  • 2,113
  • 3
  • 20
  • 28