Prefer large titles for single View Controller
Asked Answered
P

4

9

How can I set a large title for a single View Controller embedded in a Navigation Controller? Normally I am only able to set large titles for a whole Navigation Controller including all View Controllers but i only want one to display a large title.

self.navigationController?.navigationBar.prefersLargeTitles = true
Pug answered 1/12, 2018 at 22:0 Comment(0)
K
14

While both of the other answers do the trick, a cleaner way to achieve this is by doing the following in the UIViewController you want:

navigationItem.largeTitleDisplayMode = .always

or for the reverse effect:

navigationItem.largeTitleDisplayMode = .never

This takes away the need to keep track of a state, which in my opinion is a vast improvement

See largeTitleDisplayMode - Apple Developer Documentation for more information on the subject

Only downside is that it's iOS 11.0+

Kayne answered 18/9, 2019 at 19:26 Comment(2)
This doesn't work if you've set prefersLargeTitles on the Nav barAgglomeration
Works like a charm. Set one on first controller and set the other at detail controller.Unilingual
M
6

You can set prefersLargeTitles = true in viewWillAppear when ViewController's going to appear and prefersLargeTitles = false in viewWillDisappear when ViewController's going to disappear

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

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationController?.navigationBar.prefersLargeTitles = false
}
Methinks answered 1/12, 2018 at 22:6 Comment(0)
S
1

You can implement the logic for this viewController only. You can try something like this:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.prefersLargeTitles = true
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.navigationController?.navigationBar.prefersLargeTitles = false
}
Squadron answered 1/12, 2018 at 22:7 Comment(0)
A
1

Just place this inside viewDidLoad() of the viewController you don't want the large title when it's pushed. Example:

override func viewDidLoad() {
    super.viewDidLoad()
    // This prevents displaying large title for this vc
    navigationItem.largeTitleDisplayMode = .never
}
Antisana answered 16/8, 2023 at 4:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.