Large title is initially collapsed after push

Viewed 744

I ran into a strange problem:

  • I have a two view controllers - list, and detail.
  • When I push Detail from List, navigation bar is in colapsed mode.
  • To make title appear large, I have to scroll down.

ListViewController:

...
override func viewDidLoad() {
    super.viewDidLoad()

    setupNavigationBar()
}

private func setupNavigationBar() {
    navigationController?.navigationBar.prefersLargeTitles = true
    navigationItem.largeTitleDisplayMode = .never
}
...

DetailViewController:

...
override func viewDidLoad() {
    super.viewDidLoad()

    setupNavigationBar()
}

private func setupNavigationBar() {
    title = "Bangkok ⇄ Phuket"
    navigationController?.navigationBar.prefersLargeTitles = true
    navigationItem.largeTitleDisplayMode = .always
}
...

After some investigation, I found out that the bug oocurs only when I put certain characters in the title:

  • When the title is "Bangkok → Phuket", everything works as expected
  • When the title is "Bangkok ⇄ Phuket", pushed controller's title is collapsed

Do you guys know what are the restrictions for characters in title, or any ideas how to solve it? Thank you, any help much appreciated

2 Answers

In your DetailViewController try placing your Navigation Bar customisations into your viewWillAppear method:

override func viewWillAppear(_ animated: Bool) {
    navigationController?.navigationBar.prefersLargeTitles = true
    navigationItem.largeTitleDisplayMode = .always
}

Use navigationController?.navigationItem.largeTitleDisplayMode = .always instead of navigationItem.largeTitleDisplayMode = .always fixed the issue for me.

navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationItem.largeTitleDisplayMode = .always

This issue indicates that there are differences between navigationController.navigationItem and self.navigationItem of viewController. Be aware on using it.

Related