Large Titles Not Being Set Via Extension [Swift 5]

71 Views Asked by At

I have an extension to configure my UINavigationController with large titles which I call in ViewDidLoad of my controller;

extension UINavigationController {
    
    func configure(with title: String) {
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.title = title
        navigationItem.largeTitleDisplayMode = .automatic
    }
}

This extension doesn't seem to be called, however, when I place:

navigationController?.navigationBar.prefersLargeTitles = true

Into my ViewDidLoad, it works as expected. any ideas on why this would be?

1

There are 1 best solutions below

2
ibrahimyilmaz On

When you call navigationController?.navigationBar.prefersLargeTitles = true it refers to ViewController.navigationController.

But your extension is for UINavigationController so it refers ViewController.navigationController.navigationController

Just replace this:

navigationController?.navigationBar.prefersLargeTitles = true

With this:

navigationBar.prefersLargeTitles = true

and call this in viewDidLoad:

navigationController?.configure(with: "")