UINavigationBar text color in Swift

How do I change the color of a UINavigationBar in Swift?

Most internet applications say to do something like:

 [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; 

What I translated into

 let titleDict: NSDictionary = ["NSForegroundColorAttributeName": UIColor.whiteColor()] self.navigationController.navigationBartitleTextAttributes = titleDict //self is referring to a UIViewController 

But that will not work. I already changed the background color and buttons, but the color of the text does not change. Any ideas?

+60
text colors swift navigation uinavigationbar
Jun 25 '14 at 6:50
source share
11 answers

Use NSForegroundColorAttributeName as a key, not the string "NSForegroundColorAttributeName" .

 let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()] self.navigationController.navigationBar.titleTextAttributes = titleDict 
+101
Jun 25 '14 at 7:10
source share

You can also change all manifestations of the UINavigationController in your application in the AppDelegate.swift file. Just enter the following code into the application:didFinishLaunchingWithOptions function application:didFinishLaunchingWithOptions :

 var navigationBarAppearace = UINavigationBar.appearance() navigationBarAppearace.tintColor = UIColor.YourNavigationButtonsColor() // Back buttons and such navigationBarAppearace.barTintColor = UIColor.YourBackgroundColor() // Bar background color navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.YourTitleColor()] // Title text color 

Creds: Coderwall Blog Post

+35
Jan 14 '15 at 16:51
source share

Swift 3+

 self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white] 

Swift 4.0

 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white] 
+33
Sep 07 '16 at 23:33
source share

Swift 2.0

 self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()] 
+28
Sep 18 '15 at 15:23
source share
  //Nav Bar Title self.title = "WORK ORDER" self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()] 
+2
Jan 05 '16 at 6:25
source share

Swift 3

 UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .selected) UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.black], for: .normal) 
+2
Jan 29 '17 at 2:04 on
source share

I use like:

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let navigationBarAppearace = UINavigationBar.appearance() navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()] return true } 
+1
Jan 13 '17 at 6:17
source share

Swift 4.x:

 UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white] 
+1
Jan 10 '18 at 9:52
source share

Swift 4.2

 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] 
+1
Nov 02 '18 at 7:22
source share
  let titleDict = [NSForegroundColorAttributeName: UIColor.white] self.navigationController?.navigationBar.titleTextAttributes = titleDict 
0
Apr 20 '17 at 8:07
source share

Swift 5.1:

  let titleDict: NSDictionary = [NSAttributedString.Key.foregroundColor: UIColor.white] navigationController?.navigationBar.titleTextAttributes = titleDict as? [NSAttributedString.Key : Any] 
0
Jul 11 '19 at 10:14
source share



All Articles