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

前端之家收集整理的这篇文章主要介绍了ios – 导航控制器标题没有显示?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以,当我将我的OptionsViewController作为AppDelegate中的rootViewController时,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()

…如果我在viewDidLoad()中执行此操作,则设置OptionViewController的标题

title = "Route Options"

但是,当我将OptionsViewController推送到导航堆栈时,标题不会显示.

即如果我开始使用不同的视图作为AppDelegate中的rootViewController:

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()

在HomeViewController中,我按下这样推动我的OptionViewController:

let optionsVC = OptionsViewController()
    navigationController?.pushViewController(optionsVC,animated: true)

标题没有出现!

我为标题显示的唯一方法是通过执行(在OptionViewController中)

navigationController?.navigationBar.topItem?.title = "Route Options"

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

如果有人能告诉我如何设置标题,以便它被导入到导航栏的中间,当它被推到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
    }

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)
    }
}

OptionsViewController.swift

class OptionsViewController: UIViewController,DestinationDelegate,SearchBarCancelDelegate,UISearchBarDelegate,CLLocationManagerDelegate {
    override func viewDidLoad() {
        self.title = "Route Options"
    }

解决方法

您需要将navigationItem.title设置为所需的值.如果你想要一个图像,你可以设置navigationItem.titleView
override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "Your title here"
 }
原文链接:https://www.f2er.com/iOS/333938.html

猜你在找的iOS相关文章