2

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.

Lord Vermillion
  • 5,264
  • 20
  • 69
  • 109

6 Answers6

5

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)

Gaurav Bishnoi
  • 7,963
  • 2
  • 13
  • 12
ajpallares
  • 777
  • 4
  • 16
  • Then i don't get the < icon. – Lord Vermillion Nov 05 '15 at 11:02
  • That is right. I don't know if it is possible with the < icon. I'll keep trying ;) – ajpallares Nov 05 '15 at 11:03
  • You're overriding the default button, you cant keep the arrow, you have to in that case add the arrow as an image for the new button. – Arbitur Nov 05 '15 at 11:04
  • Exactly, in the solution I provided you override the button, son you lose the < icon. That's right. See the edited answer – ajpallares Nov 05 '15 at 11:05
  • This syntax changed a bit with Swift 3/4. Essentially you need to change the argument in your action to `#selector(backTapped)` and add `@objc` before your function declaration. src: https://stackoverflow.com/questions/39205613/selector-syntax-for-swift-3-0 – Braden Holt Nov 13 '18 at 20:35
2

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)
}
Ramprasath Selvam
  • 3,868
  • 3
  • 25
  • 41
1

Just Use this on the viewcontroller you have to pop

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

Or

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(false)
    UIView.setAnimationsEnabled(false)
}

Animation for view is totally removed. And After poped don't forget to add UIView.setAnimationsEnabled(true) on your next viewcontroller.

Gobi
  • 83
  • 7
0

viewWillDisappear() doesnt handle the animation, its just.

If you're using a UINavigationController

self.navigationController?.popViewController(animated: false)

If you're just using UIViewController

self.dismissViewControllerAnimated(false, completion: nil)
craft
  • 2,017
  • 1
  • 21
  • 30
Arbitur
  • 38,684
  • 22
  • 91
  • 128
0

If you want to custom animation maybe try this:

override func viewDidLoad() {
    super.viewDidLoad()

navigationController?.navigationBar.tintColor = UIColor.white
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "◁", style: .plain, target: self, action: #selector(backTapped(sender:)))

}

// with fade animations

@objc func backTapped(sender: UIBarButtonItem) {
    let transition: CATransition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeIn)
    transition.type = CATransitionType.fade
    self.navigationController!.view.layer.add(transition, forKey: nil)
    navigationController?.popViewController(animated: false)
  }

// "◁" added this way: Edit -> Emoji & Symbols

Stas
  • 87
  • 8
-4

You can try this

override func viewWillDisappear(animated: Bool) {
    self.navigationController?popViewControllerAnimated(false)
}
rnsjtngus
  • 225
  • 1
  • 8