text - custom - swift navigation bar solid color
Color de texto UINavigationBar en Swift (9)
¿Cómo cambiaría el color de UINavigationBar
en Swift?
La mayoría de las cosas en línea dicen hacer algo como:
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
Lo cual traduje a
let titleDict: NSDictionary = ["NSForegroundColorAttributeName": UIColor.whiteColor()]
self.navigationController.navigationBartitleTextAttributes = titleDict
//self is referring to a UIViewController
Pero no funciona. Ya cambié los colores de fondo y botón, pero el color del texto no cambia. ¿Algunas ideas?
Swift 4.x:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
También puede cambiar todas UINavigationController
apariencias de UINavigationController
en su aplicación dentro del archivo AppDelegate.swift
. Simplemente coloque el siguiente código dentro de la application:didFinishLaunchingWithOptions
función application:didFinishLaunchingWithOptions
:
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.YourNavigationButtonsColor() // Back buttons and such
navigationBarAppearace.barTintColor = UIColor.YourBackgroundColor() // Bar''s background color
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.YourTitleColor()] // Title''s text color
Use NSForegroundColorAttributeName
como clave, no cadena "NSForegroundColorAttributeName"
.
let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationController.navigationBar.titleTextAttributes = titleDict
Yo uso como:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
return true
}
Swift 2.0
self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
Swift 3+
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
Swift 4.0
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
Swift 3
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.black], for: .normal)
//Nav Bar Title
self.title = "WORK ORDER"
self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
let titleDict = [NSForegroundColorAttributeName: UIColor.white]
self.navigationController?.navigationBar.titleTextAttributes = titleDict