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
}
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.