Search code examples
iosswiftuinavigationcontrollerios11

why my iOS11 navigationController doesn't to work


I only want to show a large title.
My ViewController.swift look like:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        title = "Hello world"
        self.navigationController?.navigationBar.prefersLargeTitles = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

But it doesn't work.


Solution

  • to show large title in navigationbar you need to make largeTitleDisplayMode .always add below lines of code into your viewDidLoad also don't forget to check ios11 or not

       title = "Hello world"
    
       if #available(iOS 11.0, *) {
            self.navigationController?.navigationBar.prefersLargeTitles = true
            self.navigationItem.largeTitleDisplayMode = .always
        }
    

    and your UIViewController should have a UINavigationController If your UIViewController not embed in UINavigationController do the following steps

    Open your storyboard -> select your view controller -> Editor -> Embed In -> NavigationController