I think all day to change the navigation Bar title color, but it doesn\'t work. this is my code:
var user: User? {
didSet {
navigationItem.title = us
Swift 5/Xcode 11
Write this code as extension from UINavigationBar
extension UINavigationBar {
func customNavigationBar() {
// color for button images, indicators and etc.
self.tintColor = UIColor.Custom.mainAppColor
// color for background of navigation bar
// but if you use larget titles, then in viewDidLoad must write
// navigationController?.view.backgroundColor = // your color
self.barTintColor = .white
self.isTranslucent = false
// for larget titles
self.prefersLargeTitles = true
// color for large title label
self.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.someColor]
// color for standard title label
self.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.someColor]
// remove bottom line/shadow
self.setBackgroundImage(UIImage(), for: .default)
self.shadowImage = UIImage()
}
}
then in AppDelegate.swift
call UINavigationBar.appearance().customNavigationBar()
in didFinishLaunchingWithOptions
function
For iOS 13 you have to change the color in the appearance property and for older iOS versions you can do it directly in the navigation bar property.
if #available(iOS 13.0, *) {
navigationController?.navigationBar.standardAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
} else {
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}
The didSet is called if your set the user, maybe you're setting the user's name variable and expecting the program to enter didSet. try setting the user.
And if you want to change the color of the text when the navigation title is changed to the name of the user just call this code.
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.red]
var user: User? {
didSet {
navigationItem.title = user?.name
}
}
override func viewDidLoad() {
super.viewDidLoad()
let newUser = User()
newUser.name = "John Doe"
user = newUser
}
in objetive c:
[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"ArialMT" size:16.0], NSFontAttributeName,nil]];
To customize the appearance of a navigation bar you need to use UINavigationBarAppearance:
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.foregroundColor: UIColor.red]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.red]
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
import UIKit
class ProfileViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationBar()
}
func configureNavigationBar() {
navigationItem.title = "Profile"
let textChangeColor =[NSAttributedString.Key.foregroundColor:UIColor.black]
navigationController?.navigationBar.titleTextAttributes = textAttributes
navigationItem.rightBarButtonItem?.tintColor = .white
navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "arrow_right").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.rightBarButtonItem?.tintColor = .white
}
}