1

I have a simple navigation bar, and title that i set through storyboard. I have set a white color for title.

enter image description here

Then i have increased the size.

navigationController?.navigationBar.prefersLargeTitles = true

enter image description here

This automatically changed the color/font and all properties of my title.

I have tried several ways (as we normally change all attributes), but it looks like non of them work on this after i have increased size.

How to customize title in this case?

Note*: I am asking about color for specific NavVC/VC title. Not in AppDelegate for all VC.

Answer: Please follow: Changing the text color of a navigation bar title when "prefersLargeTitles" is set to true

3 Answers3

3

Use the below code to change the color

UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.orange]

Try like this if the other one is not working;

if #available(iOS 11.0, *) {
        //To change iOS 11 navigationBar largeTitle color

        UINavigationBar.appearance().prefersLargeTitles = true
        UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.white]

    } else {
        // for default navigation bar title color
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.white]

    }
vivekDas
  • 1,248
  • 8
  • 12
  • Second one is also not working. Just in case, this is a first page. I have tried to access navBarItem, did not work again. It looks like that line of code in question, block everything. – Saleh Ahmadzada Aug 02 '18 at 16:54
2

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

enter image description here

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

enter image description here

Output is:

enter image description here

Nikunj Kumbhani
  • 3,758
  • 2
  • 26
  • 51
1

Please add this code in viewDidLoad() to change the color/font of the title.

 if #available(iOS 11.0, *) {
            UINavigationBar.appearance().largeTitleTextAttributes =
                [NSAttributedStringKey.foregroundColor: UIColor.green,
                 NSAttributedStringKey.font:UIFont.boldSystemFont(ofSize: 20)]
        } else {
            // Fallback on earlier versions
        }
user28
  • 89
  • 5