iOS: Removing UINavigationBar animation

2019-03-04 11:40发布

问题:

Our app has an UINavigationBar with an image on it. When we segue (push) to another screen then click the back button the image on the Navigation Bar seems to animate from left to right as it reappears. This is a little distracting. How can you remove this back button animation?

We tried changing the segue Animates setting but this changes both the push animation and not the back animation.

Our Nav Bar code:

    let logoImage:UIImage = UIImage(named: "ABC")!
    viewController.navigationItem.titleView = UIImageView(image: logoImage)

回答1:

Figured this out in large part due to this answer https://stackoverflow.com/a/8602982/47281

Create a custom Nav Bar and override popItem:

class MyNavigationBar: UINavigationBar {
    override func popItem(animated: Bool) -> UINavigationItem? {
        return super.popItem(animated: false)
    }
}

Entered MyNavigationBar as the Navigation Bar class for our Navigation Controller via the Storyboard:

Note I did not override NavigationController popViewControllerAnimated as in the linked answer.



回答2:

You can do this:

override func viewDidLoad() {
    super.viewDidLoad()
    let logoImage: UIImage = UIImage(named: "ABC")!
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: logoImage, style: .plain, target: self, action: #selector(backBtnPressed))
}

And then create a method to handle the tap on the button

func backBtnPressed(){
    _ = self.navigationController?.popViewController(animated: false)
}