UINavigationBar - change title color - ios

I try to change the color of the navigationBar title and tried the following:
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.barTintColor = UIColor.red
navigationBarAppearace.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
navigationBarAppearace.tintColor = UIColor.white
Everything works fine but the color of the title is not changing. It is still black. I thought this line will change it
navigationBarAppearace.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
Is this wrong?

Try this in your controller, you have to set the color once for large and normal title.
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.red]
navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.red]

Related

UISearchController change color

I use this code to change my navigation bar appearance which I put it in AppDelegate:
UINavigationBar.appearance().barTintColor = UIColor(hex: "E1354A")
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Also I use UISearchController which I add programmatically. But when I push it the color of navigation bar and search controller are changed in black. I don't understand why it happens and how I can prevent it?
[][1
I've found a solution. I put in viewDidLoad:
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = UIColor(hex: "E1354A")
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
let searchField = searchController.searchBar.searchTextField
searchField.backgroundColor = .systemBackground

NavigationBar backgroundColor not coloring statusbar background

I am having some problems coloring the navigationBar. I have tried using different methods from some different tutorials but nothing seems to work the way that I want it to work.
I am using the following code in my AppDelegate:
let navAppeareance = UINavigationBarAppearance()
navAppeareance.configureWithOpaqueBackground()
navAppeareance.backgroundColor = .systemRed
//Setup buttons
let buttonDone = UIBarButtonItemAppearance(style: .done)
buttonDone.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
navAppeareance.doneButtonAppearance = buttonDone
let buttonPlain = UIBarButtonItemAppearance(style: .plain)
buttonPlain.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
navAppeareance.buttonAppearance = buttonPlain
//Set appearances
UINavigationBar.appearance().standardAppearance = navAppeareance
UINavigationBar.appearance().scrollEdgeAppearance = navAppeareance
UINavigationBar.appearance().backgroundColor = .systemRed
The above code yields the following result:
I want the statusbar to be the same color as the navigationBar, but it takes the color of the backgroundColor of the view.
use this in AppDelegate
let navBarAppearnce = UINavigationBar.appearance()
navBarAppearnce.barTintColor = UIColor.red // the color you want

how to change navigationitem title color

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 = user?.name
observeMessages()
}
}
I use didSet to show the title on the navigation title.
Add this in your code . .
let textAttributes = [NSForegroundColorAttributeName:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
SWIFT 4:
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
SWIFT 4.2+:
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
Keeping all the other attributes of the title:
If you just want change the color you could do like this:
if var textAttributes = navigationController?.navigationBar.titleTextAttributes {
textAttributes[NSAttributedString.Key.foregroundColor] = UIColor.red
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
The title color of Navigation Bar can be changed in Storyboard.
Go to Attributes inspector of Navigation Controller > Navigation Bar and set the desired color in Title Color menu.
Solution for iOS 13
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
Swift 4
set this first
navigationController?.navigationBar.barStyle = .default
then one of those should work
navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
or
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
Swift 5
navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
or
navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
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]
}
in objetive c:
[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], NSForegroundColorAttributeName,
[UIFont fontWithName:#"ArialMT" size:16.0], NSFontAttributeName,nil]];
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
}
If you set your navigation bar's title to prefer large titles, like so:
navigationBar.prefersLargeTitles = true
then you need to use the largeTitleTextAttributes property and not the titleTextAttributes property. If you set your nav title to be a large title, the titleTextAttribute is not the correct property to use. Use the largeTitleTextAttributes property, like so:
navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
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
you can just add this line of code in your AppDelegate in the func
didFinishLaunchingWithOptions
Code you will add to change title color:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)]
you can add this line as well if you wanna change the backButton color
UINavigationBar.appearance().tintColor = #colorLiteral(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)
Finally you can add this line to change the background color:
UINavigationBar.appearance().barTintColor = #colorLiteral(red: 0.2000651062, green: 0.1960035861, blue: 0.2000851929, alpha: 1)
Note: You can change the values dependent on the color you want
and have a happy coding day
Swift 4
create project and test this with ViewController easy to use
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
}
}

How to change back button color in nav bar?

Is there a way to change only the left side back button color in an app with a navigation controller?
There are plenty of examples changing colors in the navbar but those all affect the navbar title as well. I don't want to change the title. Just the back button (text + chevron) color.
Use Below To Change Back Button Color:
navigationController?.navigationBar.tintColor = UIColor.red
To Change Title Color of The Navigation Bar Use:
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
UINavigationBar.appearance().backgroundColor = UIColor.greenColor()
UIBarButtonItem.appearance().tintColor = UIColor.magentaColor()
Since iOS 7.0 UITextAttributeTextColor was replaced by NSForegroundColorAttributeName
UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.blueColor()]
UITabBar.appearance().backgroundColor = UIColor.yellowColor();
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.barTintColor = UIColor.black
self.navigationController?.navigationBar.titleTextAttributes = UIColor.blue
}

Navigation Bar change color when running

When I switch to another screen, the navigation bar (white) turns gray (if I put another color took a darker shade of the same color)
This is my code to choose the color
self.navigationController!.navigationBar.barTintColor = UIColor.whiteColor()
self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blackColor()]
self.navigationController!.navigationBar.translucent = false
Any idea to prevent this from happening and keep the color I want
Try below, it will surely works.
self.navigationController!.navigationBar.translucent = false;
self.navigationController!.navigationBar.barTintColor = UIColor.whiteColor()
self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blackColor()]
while going to other screen your navigation bar turns gray because, On iOS7 and later, translucent property of UINavigationBar is true by default.
Try this :
1)
var controller= UINavigationController(rootViewController:YourViewController)
controller.navigationBar.tintColor = [UIColor whiteColor];
2)
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.whiteColor()
navigationBarAppearace.barTintColor = UIColor.whiteColor()

Resources