简体   繁体   中英

Large Titles Not Being Set Via Extension [Swift 5]

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?

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: "")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM