Home > database >  Navigation bar title will not show (color for title will not work)
Navigation bar title will not show (color for title will not work)

Time:08-25

All the code looks fine, but the result is just a green navigation bar with no title showing, even though the title should show in white.

func configureUI() {
    view.backgroundColor = .white
    
    //configure NavBar
    navigationController?.navigationItem.title = "Add Personal Finance Log"
    navigationController?.navigationBar.isHidden = false
    navigationController?.navigationItem.hidesBackButton = true
    let backButton = UIBarButtonItem(image: UIImage(systemName: "chevron.left")?.withTintColor(.white, renderingMode: .alwaysOriginal), style: .plain, target: self, action: #selector(handleBack))
    navigationItem.leftBarButtonItem = backButton
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = hexStringToUIColor(hex: APP_HEXCOLOR_MAIN)
    let titleAttribute = [NSAttributedString.Key.font: UIFont.init(name: APP_FONT_MAIN, size: 22), NSAttributedString.Key.foregroundColor: UIColor.white]
    appearance.titleTextAttributes = titleAttribute as [NSAttributedString.Key : Any]
    navigationItem.standardAppearance = appearance
    navigationItem.scrollEdgeAppearance = appearance
    navigationItem.compactAppearance = appearance
}

CodePudding user response:

The code doesn't "look fine" at all. You have no business talking to the navigationController?.navigationItem. That does nothing. If you are a view controller, you talk to your navigationItem.

CodePudding user response:

Replace this line:

navigationController?.navigationItem.title = "Add Personal Finance Log"

With:

title = "Add Personal Finance Log"

Add this for change color (for example to red):

navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

In case you want large title add this:

navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
  • Related