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
Swift - Using Replacerange() to Change Certain Occurrences in a String
Scrolling Slow on Mobile/iOS When Using Overflow:Scroll
Catch an Exception for Invalid User Input in Swift
Xcode6: Run Two Instances of the Simulator
How to Implement a Swiping/Sliding Animation Between Views
What Size Should Tabbar Images Be
Add Uipickerview in Uiactionsheet from iOS 8 Not Working
Presenting a View Controller Modally from an Action Sheet's Delegate in iOS8 - iOS11
How to Delete All Objects from My Persistent Store in Core Data
Repeating Local Notification Daily at a Set Time with Swift
Reverse an Audio File Swift/Objective-C
iPhone 6 and 6 Plus Responsive Breakpoints
Dial Ussd Code from iPhone Programmatically
iOS Autolayout: Two Buttons of Equal Width, Side by Side
How to Delay a Method Call for 1 Second
How to Dismiss a Storyboard Popover
How to Implement Two Inits with Same Content Without Code Duplication in Swift