Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS disable animation for NavigationController back button

I want to disable the animation when i pop a ViewController with the back button in NavigationController.

I tried:

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(false)
}

But it still animates.

like image 668
Lord Vermillion Avatar asked May 11 '26 11:05

Lord Vermillion


2 Answers

In the Controller that you want to have that button:

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .Plain, target: self, action: #selector(backTapped))
}

@objc func backTapped(sender: UIBarButtonItem) {
    navigationController?.popViewControllerAnimated(false)
}

Take into account that this way, you will lose the < icon on the back button (since you're overriding that button). However, I think it is not possible to have a custom behaviour and the < icon at the same time (unless you add the < icon as an image by yourself)

like image 81
ajpallares Avatar answered May 14 '26 01:05

ajpallares


In which ViewController you don't want animation,
Add below lines

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIView.setAnimationsEnabled(false)
}
override func viewDidDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIView.setAnimationsEnabled(true)
}
like image 28
Ramprasath Selvam Avatar answered May 14 '26 02:05

Ramprasath Selvam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!