Iphone Navigation Bar Title Text Color

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]

Sample Image

Here is a sample code and screenshot without the largeTitleTextAttributes set, but the barStyle is set to .black

        navigationController?.navigationBar.barStyle = .black

Sample Image

Here is a screenshot without the largeTitleTextAttributes set, but the barStyle is set to .default

        navigationController?.navigationBar.barStyle = .default

Sample Image

How to change color for Navigation Bar title?

Just Simple thing go to the storyboard select Navigation Bar in Navigation Controller Scene

Sample Image

Then Set the Colour of Normal Title and Large Title from Attributes inspector like this:

Sample Image

Output is:

Sample Image



Related Topics



Leave a reply



Submit