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
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 yournavigationItem
.Replace this line:
With:
Add this for change color (for example to red):
In case you want large title add this: