我只想显示大标题
我的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()
}
}
但它没有用。
答案 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)
您的问题是,您需要在导航控制器
中添加 viewcontrollerwindow = UIWindow(frame: UIScreen.main.bounds)
let mainController = ViewController()
let navigationController = UINavigationController(rootViewController: mainController)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()