所以,当我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()
...
OptionViewController
...如果我在viewDidLoad()
执行此操作,则设置 title = "Route Options"
的标题:
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()
作为其他视图:
HomeViewController
在OptionViewController
中我按下这样的 let optionsVC = OptionsViewController()
navigationController?.pushViewController(optionsVC, animated: true)
:
OptionViewController
标题没有出现!
我设置标题的唯一方法就是(navigationController?.navigationBar.topItem?.title = "Route Options"
)
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
}
但是它显示为后退按钮而不是中间,这不是我想要的。
如果有人能够告诉我如何设置标题,以便当它被推到navigationController堆栈时它位于导航栏的中间位置,这将是非常棒的!
AppDelegate.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)
}
}
HomeViewController.swift
class OptionsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,
DestinationDelegate, SearchBarCancelDelegate,UISearchBarDelegate,
CLLocationManagerDelegate {
override func viewDidLoad() {
self.title = "Route Options"
}
OptionsViewController.swift
答案 0 :(得分:3)
您需要将navigationItem.title设置为所需的值。如果你想要一个图像,你可以设置navigationItem.titleView
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Your title here"
}
答案 1 :(得分:3)
首先,您需要设置UINavigationBar
颜色和文字颜色。
请在didFinishLaunchingWithOptions
中尝试此操作。
let barAppearance = UINavigationBar.appearance()
barAppearance.barTintColor = UIColor.blue
barAppearance.tintColor = UIColor.white
barAppearance.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
如果你想在后退按钮后删除字符串 添加这些
let barItemAppearace = UIBarButtonItem.appearance()
barItemAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
只需在viewDidLoad()
或
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.title = "Your Title"
}
答案 2 :(得分:2)
对于根据标题来到这里的其他人,不要忘记将IB中的ViewController类设置为适当的Swift文件。
在这样做之后,我可以使用
设置标题而没有问题self.navigationItem.title = "my title"
或
self.title = "my title"
答案 3 :(得分:1)
试一试:
在HomeViewController
:
let optionsVC = OptionsViewController()
navigationController?.viewControllers = [optionsVC]
在您的OptionsViewController
:
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.isTranslucent = false
navigationItem.title = "Your Title"
}
答案 4 :(得分:0)
只需添加以下行即可设置导航项目的标题
self.title = "Title 1"
答案 5 :(得分:0)
使用 Objective-C 时,我还遇到了一个问题,它没有显示标题,而是转到下一个场景,标题显示在后退按钮旁边。
我真的不知道为什么,但是我通过在 Swift 中而不是 Objective-C 中对该场景的相对ViewController进行编程来解决了。
在那之后,使用命令就足够了:
self.title = "My Title"
所以我能够使用if语句或其他方法以编程方式编写想要的内容。
这可能对使用 Objective-C 存在此问题的人有用。