skip to Main Content

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
}

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search