Does the title of the navigation controller not display?

So, when I have OptionsViewControllerboth rootViewControllerin 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 header OptionViewControllerworks when I do it viewDidLoad():

    title = "Route Options"

enter image description here

But when I click OptionsViewControlleron a stack of navigation, the title is not displayed.

those. If I start with another idea how rootViewControllerto 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 HomeViewControllerI push her OptionViewControlleras follows:

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

The title is not displayed!

enter image description here

The only way I could handle - make it (in OptionViewController)

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

But it appears as a back button, not a middle button, but that’s not what I want.

enter image description here

- , , , 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, UITableViewDelegate, UITableViewDataSource,
    DestinationDelegate, SearchBarCancelDelegate,UISearchBarDelegate,
    CLLocationManagerDelegate {
    override func viewDidLoad() {
        self.title = "Route Options"
    }
+6
5

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

navigationItem.title . , navigationItem.titleView

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "Your title here"
 } 
+1

Just add the line below to set the title for the navigation item.

self.title = "Title 1"
+1
source

Try:

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"
    }
0
source

For other header-based contributors, remember to set the ViewController class to IB in the corresponding Swift file.

After that, I was able to set the title without problems using

self.navigationItem.title = "my title"

or

self.title = "my title"
0
source

Source: https://habr.com/ru/post/1016739/


All Articles