简体   繁体   中英

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.

  let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60))
        navigationBar.backgroundColor = UIColor(red: 46.0/255.0, green: 145.0/255.0, blue: 245.0/255.0, alpha: 1.0)
        
        self.navigationItem.title = "Add Title"
        self.view .addSubview(navigationBar)
    if #available(iOS 11.0, *) {
        self.navigationController?.navigationBar.prefersLargeTitles = true
        self.navigationItem.largeTitleDisplayMode = .always
    } else {
        // Fallback on earlier versions
    }

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

Firstly, ensure your ViewController is inside a UINavigationController .

Proceed to check for IOS 11

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

} else { ... }

Your problem is, you need to add your viewcontroller in a navigation controller

window = UIWindow(frame: UIScreen.main.bounds)
let mainController = ViewController()
let navigationController = UINavigationController(rootViewController: mainController)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()

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