导航控制器标题没有显示?

14w*_*wml 10 uiviewcontroller uinavigationcontroller ios swift

所以,当我有我OptionsViewController的rootViewController在AppDelegate didFinishLaunchingWithOptions...

let rootVC = OptionsViewController()
        let navigationController = UINavigationController(rootViewController: rootVC)
        navigationController.navigationBar.barTintColor = .white
        navigationController.navigationBar.isTranslucent = false
        navigationController.navigationBar.tintColor = .black
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window!.rootViewController = navigationController
        self.window?.makeKeyAndVisible()
Run Code Online (Sandbox Code Playgroud)

... OptionViewController如果我这样做,设置作品的标题viewDidLoad():

    title = "Route Options"
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是当我按下OptionsViewController导航堆栈时,标题不显示.

也就是说,如果我开始瓦特/不同的视图作为rootViewController在AppDelegate:

 let rootVC = HomeViewController()
    let navigationController = UINavigationController(rootViewController: rootVC)
    navigationController.navigationBar.barTintColor = .white
    navigationController.navigationBar.isTranslucent = false
    navigationController.navigationBar.tintColor = .black
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window!.rootViewController = navigationController
    self.window?.makeKeyAndVisible()
Run Code Online (Sandbox Code Playgroud)

在HomeViewController我推动我OptionViewController这样:

    let optionsVC = OptionsViewController()
    navigationController?.pushViewController(optionsVC, animated: true)
Run Code Online (Sandbox Code Playgroud)

标题没有出现!

在此输入图像描述

我为标题出现的唯一方法就是做(中OptionViewController)

navigationController?.navigationBar.topItem?.title = "Route Options"
Run Code Online (Sandbox Code Playgroud)

但它显示为后退按钮而不是中间,这不是我想要的.

在此输入图像描述

如果有人能告诉我如何设置标题,以便它被导入到导航栏的中间,当它被推到navigationController堆栈上时会很棒!

码

AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate {
     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let rootVC = HomeViewController()
        let navigationController = UINavigationController(rootViewController: rootVC)
        let barAppearance = UINavigationBar.appearance()
        barAppearance.barTintColor = UIColor.blue
        barAppearance.tintColor = UIColor.white
        barAppearance.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window!.rootViewController = navigationController
        self.window?.makeKeyAndVisible()

        return true
    }
Run Code Online (Sandbox Code Playgroud)

HomeViewController.swift

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, DestinationDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let optionsVC = OptionsViewController()
            self.definesPresentationContext = false //else going to try and present optionVC on homeVC when in optionVC
            navigationController?.pushViewController(optionsVC, animated: true)
        }
        tableView.deselectRow(at: indexPath, animated: true)
    }
}
Run Code Online (Sandbox Code Playgroud)

OptionsViewController.swift

class OptionsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,
    DestinationDelegate, SearchBarCancelDelegate,UISearchBarDelegate,
    CLLocationManagerDelegate {
    override func viewDidLoad() {
        self.title = "Route Options"
    }
Run Code Online (Sandbox Code Playgroud)

Jus*_*inM 6

您需要将 navigationItem.title 设置为所需的值。如果你想要一张图片,你可以设置 navigationItem.titleView

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "Your title here"
 } 
Run Code Online (Sandbox Code Playgroud)


Sur*_*gch 6

对于其他根据标题来到此处的人,请不要忘记将 IB 中的 ViewController 类设置为适当的 Swift 文件。

这样做之后,我可以使用以下命令毫无问题地设置标题

self.navigationItem.title = "my title"
Run Code Online (Sandbox Code Playgroud)

或者

self.title = "my title"
Run Code Online (Sandbox Code Playgroud)