简体   繁体   中英

How to change color for Navigation Bar title?

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

在此处输入图片说明

Then i have increased the size.

navigationController?.navigationBar.prefersLargeTitles = true

在此处输入图片说明

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

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]

    }

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
        }

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

在此处输入图片说明

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

在此处输入图片说明

Output is:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM