how to change navigationitem title color
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
}
NavigationBar bar, tint, and title text color in iOS 8
In AppDelegate.swift
, in application(_:didFinishLaunchingWithOptions:)
I put the following:
UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
(For Swift 4 or earlier use NSAttributedStringKey
instead of NSAttributedString.Key
)
For titleTextAttributes
, the docs say:
You can specify the font, text color, text shadow color, and text
shadow offset for the title in the text attributes dictionary
How can I change the NavBar title text color in swift 4?
You can change the UINavigationBar title color by setting the foregroundColor
property on titleTextAttributes
.
As documented here:
The titleTextAttributes property specifies the attributes for
displaying the bar’s title text. You can specify the font, text color,
text shadow color, and text shadow offset for the title in the text
attributes dictionary using the font , foregroundColor , and shadow
keys, respectively.
So you will have the same effect doing this:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.orange]
Changing the text color of a navigation bar title when prefersLargeTitles is set to true
There is a new UINavigationBar property "largeTitleTextAttribute" that should help with this.
largeTitleTextAttribute
Here is a sample code you can add to your view controllers viewDidLoad method
navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
Here is a sample code and screenshot without the largeTitleTextAttributes set, but the barStyle is set to .black
navigationController?.navigationBar.barStyle = .black
Here is a screenshot without the largeTitleTextAttributes set, but the barStyle is set to .default
navigationController?.navigationBar.barStyle = .default
How to change color for Navigation Bar title?
Just Simple thing go to the storyboard select Navigation Bar in Navigation Controller Scene
Then Set the Colour of Normal Title and Large Title from Attributes inspector like this:
Output is:
Related Topics
How to Get Device Make and Model on Ios
Library? Static? Dynamic? or Framework? Project Inside Another Project
How to Make a Simple Collection View With Swift
Communicating and Persisting Data Between Apps With App Groups
How to Detect When Someone Shakes an Iphone
Difference Between 'Yyyy' and 'Yyyy' in Nsdateformatter
Swift: Extra Argument 'Error' in Call
Create Singleton Using Gcd'S Dispatch_Once in Objective-C
Can't Use Swift Classes Inside Objective-C
Libc++Abi.Dylib: Terminating With Uncaught Exception of Type Nsexception (Lldb)
Exc_Bad_Access Signal Received
Ios 7: Uitableview Shows Under Status Bar