iOS disable animation for NavigationController back button

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 15:32:30

问题


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


来源:https://stackoverflow.com/questions/33542009/ios-disable-animation-for-navigationcontroller-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!