-->

iOS disable animation for NavigationController bac

2019-08-09 03:57发布

问题:

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.

回答1:

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: "backTapped:")
}

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)



回答2:

viewWillDisappear() doesnt handle the animation, its just.

If you're using a UINavigationController

self.navigationController?popViewControllerAnimated(false)

If you're just using UIViewController

self.dismissViewControllerAnimated(false, completion: nil)


回答3:

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



回答4:

You can try this

override func viewWillDisappear(animated: Bool) {
    self.navigationController?popViewControllerAnimated(false)
}