Akruti
Akruti

Reputation: 183

Navigation Bar's Default behaviour to go back is not working

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.

Upvotes: 1

Views: 1463

Answers (3)

Patrick R
Patrick R

Reputation: 6857

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.

Upvotes: 1

mugx
mugx

Reputation: 10105

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
    }
}

Upvotes: 1

Ganesh Manickam
Ganesh Manickam

Reputation: 2139

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

Upvotes: 0

Related Questions