Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation controller title not showing up?

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"

enter image description here

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!

enter image description here

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.

enter image description here

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!

Code

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"
    }
like image 326
14wml Avatar asked Apr 25 '17 02:04

14wml


1 Answers

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"
 } 
like image 178
JustinM Avatar answered Sep 16 '22 13:09

JustinM