iOS: Removing UINavigationBar animation

2019-03-04 12:10发布

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)

2条回答
Animai°情兽
2楼-- · 2019-03-04 12:33

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:

enter image description here

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

查看更多
趁早两清
3楼-- · 2019-03-04 12:33

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)
}
查看更多
登录 后发表回答