Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back button animation with large title jumps

We have two UIViewController with an UINavigationController.

In the first presented VC inside of viewWillAppear(_ animated: Bool) we do:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if #available(iOS 11.0, *) {
            navigationController?.navigationBar.prefersLargeTitles = true
            navigationController?.navigationItem.largeTitleDisplayMode = .always
        }
 ....

Inside of the second VC we deactive that behaviour with inside of viewWillAppear(_ animated: Bool):

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if #available(iOS 11.0, *) {
        navigationController?.navigationBar.prefersLargeTitles = false
    }
...

The transition animation to the second VC is smooth while tapping automatic generated back button causes the navigation controller title to create a strange jump to large title instead of the normal grow to large title animation as it does for example in the Messages App.

If i tap the tabbar icon as "back" operation, it does the right transition animation.

Any idea what could cause that issue or how i can fix it?

like image 606
BennX Avatar asked Sep 20 '17 09:09

BennX


3 Answers

on the second view controller set the largeTitleDisplayMode to .never you won't need to set the prefersLargeTitles to false.

To clarify things here, you've to set the largeTitleDisplayMode directly for the navigationItem of the view controller, not the navigation controller!

self.navigationItem.largeTitleDisplayMode = .never // This fixes the issue
self.navigationController?.navigationItem.largeTitleDisplayMode = .never // This doesn't work / Title will stay large
like image 198
dave Avatar answered Nov 15 '22 19:11

dave


One should make force layout of navigation bar right after switching off large title

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    
    navigationController?.navigationBar.prefersLargeTitles = false
    navigationController?.navigationBar.layoutIfNeeded()
}

This cancels out large navigation title immediately.

like image 39
malex Avatar answered Nov 15 '22 20:11

malex


@dave's answer worked for me! Thanks! Here's the code that I used in its entirety:

FirstViewController:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if #available(iOS 11.0, *) {
        navigationItem.largeTitleDisplayMode = .always
            navigationController?.navigationBar.prefersLargeTitles = true
        }
    }
}

SecondViewController:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if #available(iOS 11.0, *) {
            navigationItem.largeTitleDisplayMode = .never
        }
    }
}
like image 34
Mark Jeschke Avatar answered Nov 15 '22 20:11

Mark Jeschke