简体   繁体   English

为什么我的 iOS11 导航控制器不起作用

[英]why my iOS11 navigationController doesn't to work

I only want to show a large title .我只想显示一个大标题
My ViewController.swift look like:我的ViewController.swift看起来像:

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显示在导航栏,你需要做出大标题largeTitleDisplayMode .always添加下面的代码行到您viewDidLoad也不要忘记检查ios11或不

   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并且您的UIViewController应该有一个UINavigationController如果您的 UIViewController 未嵌入 UINavigationController 中,请执行以下步骤

Open your storyboard -> select your view controller -> Editor -> Embed In -> NavigationController打开你的故事板 -> 选择你的视图控制器 -> 编辑器 -> 嵌入 -> NavigationController

Firstly, ensure your ViewController is inside a UINavigationController .首先,确保您的ViewController位于UINavigationController

Proceed to check for IOS 11继续检查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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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