Navigationbar Bar, Tint, and Title Text Color in iOS 8

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 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
}

How to change color of navigation title

public var navigationBarColor = UIColor() {
didSet {
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]
}
}

iOS13 - problem with navigationBar title color

In iOS13 you need to set the title color on the UINavigationBarAppearance object - try adding this line to your code:

appearance.titleTextAttributes = [.foregroundColor: myAppLabelColor]
appearance.largeTitleTextAttributes = [.foregroundColor: myAppLabelColor]

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 navigation bar color in Swift

Navigation Bar:

navigationController?.navigationBar.barTintColor = UIColor.green

Replace greenColor with whatever UIColor you want, you can use an RGB too if you prefer.

Navigation Bar Text:

navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]

Replace orangeColor with whatever color you like.

Tab Bar:

tabBarController?.tabBar.barTintColor = UIColor.brown

Tab Bar Text:

tabBarController?.tabBar.tintColor = UIColor.yellow

On the last two, replace brownColor and yellowColor with the color of your choice.



Related Topics



Leave a reply



Submit