Reputation: 1594
I'm using the new enlarged navigation bar titles in iOS 11. But I can't seem to be able to change the textColor.
I tried doing:
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
This didn't do anything. Any ideas?
Upvotes: 12
Views: 14451
Reputation: 1050
Swift 5.6.1
In my Swift 5.6.1 and iOS 15.6.1 the following code worked only.
Add the following codes in ViewDidLoad()
let appearance = UINavigationBarAppearance(idiom: .phone)
// Add the color you want in your title
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
// Add the color you want as your navigation bar color. Otherwise it shows White by default
appearance.backgroundColor = .purple
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
Upvotes: 2
Reputation: 1
let largeTitleTextAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.gray20, .font: UIFont.systemFont(ofSize: 24.0, weight: .bold)]
if #available(iOS 15, *) {
let navigationBar = navigationController.navigationBar
let appearance = navigationBar.standardAppearance
appearance.largeTitleTextAttributes = largeTitleTextAttributes
navigationBar.standardAppearance = appearance
navigationBar.scrollEdgeAppearance = appearance
} else {
navigationController.navigationBar.largeTitleTextAttributes = largeTitleTextAttributes
}
Upvotes: 0
Reputation: 111
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
Upvotes: 1
Reputation: 2566
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
with named color
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(named: "Teal") ?? UIColor.black]
Upvotes: 2
Reputation: 5223
if (@available(iOS 11.0, *)) {
self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
self.navigationController.navigationBar.prefersLargeTitles = true;
// Change Color
self.navigationController.navigationBar.largeTitleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
} else {
// Fallback on earlier versions
}
Upvotes: 2
Reputation: 171
I think it's still a bug in Xcode 9 beta 6.
I found different "solutions" for it:
if #available(iOS 11.0, *) { UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue] }
if #available(iOS 11.0, *) { self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 31, weight: UIFont.Weight.bold) ] }
Hope it helps you.
Regards!
Upvotes: 17
Reputation: 761
self.navigationController.navigationBar.largeTitleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
Upvotes: 23