Reputation: 4166
So when I have my OptionsViewController
as the rootViewController
in the 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()
...setting the title of the OptionViewController
works if I do this in viewDidLoad()
:
title = "Route Options"
But when I push OptionsViewController
onto the navigation stack the title doesn't show up.
I.e. if I start w/ a different view as the rootViewController
in 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()
And in HomeViewController
I push my OptionViewController
like this:
let optionsVC = OptionsViewController()
navigationController?.pushViewController(optionsVC, animated: true)
The title does not show up!
The only way I've managed for the title to show up is by doing (in OptionViewController
)
navigationController?.navigationBar.topItem?.title = "Route Options"
But it shows up as the back button rather than in the middle, which is not what I want.
If anyone could tell me how I could set the title so that it is on the middle of the navigation bar when it is pushed onto the navigationController stack that would be great!
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, UITableViewDelegate, UITableViewDataSource,
DestinationDelegate, SearchBarCancelDelegate,UISearchBarDelegate,
CLLocationManagerDelegate {
override func viewDidLoad() {
self.title = "Route Options"
}
Upvotes: 19
Views: 20900
Reputation: 61
All you need is initialisation navigationItem.title
by string containing any printable symbols in viewDidLoad
function. String like ""
or " "
will not worked.
Like this:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "."
}
Then code navigationItem.title = "Any string"
will work well anywhere in your ViewController.
This bag was fixed in iOs 16.2
Upvotes: 0
Reputation: 335
Using Objective-C I also had the problem that it didn't show me the title but going to the next Scene the title appeared next to the back button.
I don't really know why, but I solved it by programming the relative ViewController of that Scene in Swift instead of Objective-C.
After that, it was enough to use the command:
self.title = "My Title"
and so I was able to write what I want programmatically, using if-statement or other methodologies.
Maybe this could be useful to someone who has this problem with Objective-C.
Upvotes: 0
Reputation: 511736
For others coming here based on the title, don't forget to set your ViewController class in IB to the appropriate Swift file.
After doing that I was able to set the title without a problem using
self.navigationItem.title = "my title"
or
self.title = "my title"
Upvotes: 6
Reputation: 345
Just add the below line to set the title for navigation item
self.title = "Title 1"
Upvotes: 0
Reputation: 201
first you need to set UINavigationBar
color and text color .
try this in didFinishLaunchingWithOptions
.
let barAppearance = UINavigationBar.appearance()
barAppearance.barTintColor = UIColor.blue
barAppearance.tintColor = UIColor.white
barAppearance.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
if you want to remove the string after backbutton add these too
let barItemAppearace = UIBarButtonItem.appearance()
barItemAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
And just set your title in viewDidLoad()
or
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.title = "Your Title"
}
Upvotes: 3
Reputation: 1859
Try it:
In HomeViewController
:
let optionsVC = OptionsViewController()
navigationController?.viewControllers = [optionsVC]
And in your OptionsViewController
:
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.isTranslucent = false
navigationItem.title = "Your Title"
}
Upvotes: 3
Reputation: 2222
You need to set the navigationItem.title to desired value. If you want an image you set navigationItem.titleView
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Your title here"
}
Upvotes: 9