Der Hintergrundtext in der Statusleiste ist immer noch schwarz. Wie ändere ich die Farbe in Weiß?
// io8, Swift, Xcode 6.0.1
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()]
}
In AppDelegate.Swift
, in application(_:didFinishLaunchingWithOptions:)
stelle ich Folgendes ein:
UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
Für titleTextAttributes
sagen die docs :
Sie können Schriftart, Textfarbe, Textschattenfarbe und Text angeben Schattenversatz für den Titel im Wörterbuch für Textattribute
Ich mag die Antwort von Alex. Wenn Sie möchten, dass etwas in einer ViewController
schnell getestet wird, stellen Sie sicher, dass Sie es verwenden
viewWillAppear()
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.white
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
//nav?.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange] // Swift 4.2
}
Um die Farbe universell zu ändern, sollte dieser Code in der NavigationController
-Funktion von viewDidLoad
stehen:
class NavigationController: UINavigationController, UIViewControllerTransitioningDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Status bar white font
self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()
}
}
Um es per ViewController
zu ändern, müssten Sie die NavigationController
der ViewController
referenzieren und ähnliche Zeilen in die ViewController
-Funktion dieser viewWillAppear
schreiben.
// In Swift 4
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
Um in object-c zu arbeiten, muss ich die folgenden Zeilen in viewWillAppear
in meinem CustomViewController einfügen.
[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setTranslucent:NO];
Für Swift2.x das funktioniert:
self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
Für Swift3.x das funktioniert:
self.navigationController?.navigationBar.barTintColor = UIColor.red
Um diese Aufgabe im Storyboard auszuführen (Interface Builder Inspector)
Mit IBDesignable
können Sie dem Interface Builder Inspector weitere Optionen für UINavigationController
hinzufügen und diese im Storyboard optimieren. Fügen Sie Ihrem Projekt zunächst folgenden Code hinzu.
@IBDesignable extension UINavigationController {
@IBInspectable var barTintColor: UIColor? {
set {
navigationBar.barTintColor = newValue
}
get {
guard let color = navigationBar.barTintColor else { return nil }
return color
}
}
@IBInspectable var tintColor: UIColor? {
set {
navigationBar.tintColor = newValue
}
get {
guard let color = navigationBar.tintColor else { return nil }
return color
}
}
@IBInspectable var titleColor: UIColor? {
set {
guard let color = newValue else { return }
navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: color]
}
get {
return navigationBar.titleTextAttributes?["NSForegroundColorAttributeName"] as? UIColor
}
}
}
Dann legen Sie einfach die Attribute für UINavigationController im Storyboard fest.
Wenn Sie die Farbton- und Balkenfarbe für die gesamte App festlegen möchten, können Sie den folgenden Code zu AppDelegate.Swift in hinzufügen
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0)
navigationBarAppearace.barTintColor = UIColor(red:0.76, green:0.40, blue:0.40, alpha:1.0)
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
return true
`
Mit Swift 4 aktualisiert
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.blue
self.navigationController?.navigationBar.barStyle = UIBarStyle.black
}
In Swift 4.1 und Xcode 9.4.1
self.navigationItem.title = "your name"
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes
Schnell 4
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.barTintColor = UIColor.orange
navigationController?.navigationBar.tintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
}
Schnell 4.1
Fügen Sie eine Funktion zu viewDidLoad hinzu
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
Fügen Sie in der setup()
-Funktion Folgendes hinzu:
func setup() {
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.barStyle = .blackOpaque
navigationItem.title = "YOUR_TITLE_HERE"
navigationController?.navigationBar.barTintColor = .black
let attributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
navigationController?.navigationBar.largeTitleTextAttributes = attributes
}
Festlegen der Textfarbe des Titels der Navigationsleiste auf Weiß in Swift Version 4.2:
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
Für benutzerdefinierte Farben für TitleText
bei NavigationBar
hier ein einfacher und kurzer Code für Swift 3:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
oder
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName :UIColor.white]
Swift 4.2 Version von Alberts Antwort-
UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : UIColor.white]
in Swift 4.2
var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.white
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
self.navigationController?.navigationItem.largeTitleDisplayMode = .always
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
// unconfirmed but I assume this works:
self.navigationController?.navigationBar.barTintColor = UIColor.white
self.navigationController?.navigationBar.barStyle = UIBarStyle.black
In Swift 3 funktioniert das:
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]