Wie würde ich die Farbe der UINavigationBar
in Swift ändern?
Die meisten Dinge im Internet sagen etwas zu tun:
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
Was ich übersetzt habe
let titleDict: NSDictionary = ["NSForegroundColorAttributeName": UIColor.whiteColor()]
self.navigationController.navigationBartitleTextAttributes = titleDict
//self is referring to a UIViewController
Aber es geht nicht. Ich habe bereits die Hintergrund- und Schaltflächenfarben geändert, aber die Textfarbe ändert sich nicht. Irgendwelche Ideen?
Verwenden Sie NSForegroundColorAttributeName
als Schlüssel, nicht "NSForegroundColorAttributeName"
-Zeichenfolge.
let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationController.navigationBar.titleTextAttributes = titleDict
Sie können auch alle UINavigationController
-Erscheinungen in Ihrer App in der AppDelegate.Swift
-Datei ändern. Fügen Sie einfach folgenden Code in die application:didFinishLaunchingWithOptions
-Funktion ein:
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
Creds: Coderwalls Blogbeitrag
Swift 3+
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
Swift 4.0
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
Swift 2.0
self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
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()]
Ich benutze wie:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
return true
}
Swift 4.x:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
Swift 5.1:
let titleDict: NSDictionary = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationController?.navigationBar.titleTextAttributes = titleDict as? [NSAttributedString.Key : Any]
let titleDict = [NSForegroundColorAttributeName: UIColor.white]
self.navigationController?.navigationBar.titleTextAttributes = titleDict
Swift 4.2
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]