Search code examples
iosswiftuinavigationcontrolleruinavigationbarios-animations

Issue with UINavigationBar large title while popping to non-large translucent bar


I am implementing UINavigationBar for two UIViewController: assume ControllerA and ControllerB.

ControllerA has translucent UINavigationBar with backgroundColor = .clear property.

ControllerB has prefersLargeTitles enabled property and white background.

I should push and pop from ControllerA -> ControllerB. Here the code I've implemented in controllerA life cycle methods:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if #available(iOS 11.0, *) {
            navigationItem.largeTitleDisplayMode = .never
        }

        navigationController?.navigationBar.isTranslucent = true
        navigationController?.navigationBar.tintColor = .white
        navigationController?.navigationBar.backgroundColor = UIColor.clear

        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
        navigationController?.navigationBar.barTintColor = .clear

        navigationController?.navigationBar.titleTextAttributes = [
            .font: FontFamily.SFProRounded.bold.font(size: 18),
            .foregroundColor: UIColor.white
        ]
    }

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        if #available(iOS 11.0, *) {
            navigationController?.navigationBar.prefersLargeTitles = true
            navigationItem.largeTitleDisplayMode = .always

            navigationController?.navigationBar.largeTitleTextAttributes = [
                .font: FontFamily.SFProRounded.bold.font(size: 22),
                .foregroundColor: UIColor.black
            ]
        }
        navigationController?.navigationBar.titleTextAttributes = [
            .font: FontFamily.SFProRounded.bold.font(size: 18),
            .foregroundColor: UIColor.black
        ]
        navigationController?.navigationBar.tintColor = .black
        navigationController?.navigationBar.backgroundColor = UIColor.white
        navigationController?.view.backgroundColor = UIColor.white
        navigationController?.navigationBar.barTintColor = .white
    }

Below the issues I've had on different iOS versions:

  1. Version < iOS 13

Incorrect animation and title color while tap back button for controller dismissing. Video here: https://youtu.be/1g9esUgYDK8

  1. Version == iOS 13

Large title not moving with dismissed controller during pop animation. Video here: https://youtu.be/25k3oz2_wcE

How to solve it? Thank you in advance


Solution

  • Finally I've added

    if #available(iOS 11.0, *) {
        navigationItem.largeTitleDisplayMode = .always
    }
    

    in ViewDidLoad of ControllerB and

    if #available(iOS 11.0, *) {
        navigationItem.largeTitleDisplayMode = .never
    }
    

    in ViewDidLoad of ControllerA and it works as I expected.

    Note that you need to set prefersLargeTitles only one time, preferably when opening the application