1

It seems that I can't set navigationItem.largeTitleDisplayMode = .always unless I also set navigationBar.prefersLargeTitles = true? Is this intended behavior?

I'm having a really hard time believing apple would force me to manually set largeTitleDisplayMode = .never on every screen in the navigation controller, even ones I don't control, in order to get just a single screen to show with large titles.

lufinkey
  • 342
  • 4
  • 15
  • 2
    _Is this intended behavior?_ answer is: yes, it's intended: https://developer.apple.com/documentation/uikit/uinavigationitem/2909056-largetitledisplaymode – vpoltave Jun 22 '21 at 14:08

1 Answers1

0

You can try controlling this via UINavigationControllerDelegate.willShow calls like this.

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.delegate = self
    }
}

// Assumption: ViewController is rootViewController for the UINavigationController
extension ViewController: UINavigationControllerDelegate {
    
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        let isRootVC = (viewController === self)
        viewController.navigationItem.largeTitleDisplayMode = isRootVC ? .always : .never
    }
    
}

UPDATE

The other option would be to do this same management on viewWillAppear(_:) & viewWillDisappear(_:) calls for the rootViewController instance in your navigation stack.

Tarun Tyagi
  • 9,364
  • 2
  • 17
  • 30
  • 1
    I'd rather not have to go a hacky route like this. There are instances where I may need the navigationController delegate and can't have it get overridden by every view controller – lufinkey Jun 22 '21 at 14:03
  • This is to be done on the `rootViewController` only - Not for every UIViewController subclass within that navigation stack. – Tarun Tyagi Jun 22 '21 at 14:04
  • It still overrides the navigationController delegate, which is kind of hacky and I don't want to do as it's going to lead to other problems. I just want to be able to set `largeTitleDisplayMode` for an individual view controller and have the navigationController respect that value – lufinkey Jun 22 '21 at 14:09
  • 1
    The other option would be to do this same management on `viewWillAppear(_:)` & `viewWillDisappear(_:)` calls for the rootViewController instance in your navigation stack. No solution is pretty here. – Tarun Tyagi Jun 22 '21 at 14:24