为什么我的iOS11 navigationController无法工作

时间:2017-11-29 07:06:49

标签: ios swift uinavigationcontroller ios11

我只想显示大标题 我的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()
    }

}

但它没有用。

3 个答案:

答案 0 :(得分:0)

试试这个

  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
    }

答案 1 :(得分:0)

首先,确保您的ViewController位于UINavigationController

继续检查IOS 11

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

} else { ... }

答案 2 :(得分:0)

您的问题是,您需要在导航控制器

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