0

So say that I have 3 view controllers, A, B, and C.

A has a black background, B a white and C a blue.

All of these use NavigationControllerBars to navigate.

How do I programmatically change the color of the navBar title on these?

So that I have a white title on A, black for B and maybe Red for C.

From what I've gathered everyone uses for example:

navigationController?.navigationBar.titleTextAttributes = [NSForegroundAttributeName: UIColor.white]

I'm new to swift but to me that looks like Obj-C?

That doesn't work for me and I keep getting

Replace 'NSForegroundColorAttributeName' with 'NSForegroundColorAttributeName.rawValue'

as an error.

msanford
  • 11,803
  • 11
  • 66
  • 93
Alvarsson
  • 115
  • 3
  • 7
  • 1
    Learn how to use NSAttributedString. – El Tomato Jul 31 '17 at 08:42
  • 2
    Possible duplicate of [Changing specific text's color using NSMutableAttributedString in Swift](https://stackoverflow.com/questions/25207373/changing-specific-texts-color-using-nsmutableattributedstring-in-swift) – Dávid Pásztor Jul 31 '17 at 08:51

1 Answers1

0

I used this in my code and it compiled without any problem.

Swift 2.3

navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

Swift 3

navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223