Search code examples
iosuinavigationcontrolleruinavigationbarios14

iOS14 navigationItem.largeTitleDisplayMode = .always not work


I have a ViewController and a DetailViewController, in the ViewDidLoad of the ViewController I set the following code, the purpose is to make the ViewController always use the large title

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

In the ViewDidLoad of the DetailViewController I set the following code, the purpose is to make the DetailViewController not use the large title

navigationItem.largeTitleDisplayMode = .never

When I return from DetailViewController to ViewController, the small title is displayed instead of the large title in ViewController. This code is correct in iOS12 and iOS13. How to make the ViewController always display the large title on iOS14?

Currently using Xcode12 from the App Store


Solution

  • Use my extension:

    extension UIViewController {
    func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
    if #available(iOS 13.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
        navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
        navBarAppearance.backgroundColor = backgoundColor
    
        navigationController?.navigationBar.standardAppearance = navBarAppearance
        navigationController?.navigationBar.compactAppearance = navBarAppearance
        navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
    
        navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
        navigationController?.navigationBar.isTranslucent = false
        navigationController?.navigationBar.tintColor = tintColor
        navigationItem.title = title
    
    } else {
        // Fallback on earlier versions
        navigationController?.navigationBar.barTintColor = backgoundColor
        navigationController?.navigationBar.tintColor = tintColor
        navigationController?.navigationBar.isTranslucent = false
        navigationItem.title = title
      }
     }
    }
    

    set your navigation bar in viewWillAppear:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        configureNavigationBar(largeTitleColor: .white, backgoundColor: .yourColor, tintColor: .white, title: "YourTitle", preferredLargeTitle: true)
    }
    

    now cal the function that present your detail controller:

    @objc func DetailController(){
        let controller = DetailViewController()
        controller.navigationItem.largeTitleDisplayMode = .never
        navigationController?.pushViewController(controller, animated: true)
    }
    

    This is the result:

    enter image description here

    enter image description here