Ich möchte den Hintergrund für die Navigationsleiste auf schwarz und alle Farben in der Navigationsleiste auf weiß einstellen.
Also habe ich diesen Code benutzt:
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
NSForegroundColorAttributeName,
[UIColor whiteColor],
NSForegroundColorAttributeName,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
NSForegroundColorAttributeName,
[UIFont fontWithName:@"Arial-Bold" size:0.0],
NSFontAttributeName,
nil]];
Aber die Zurück-Taste Textfarbe, Pfeil und Balken-Taste haben immer noch die Standardeinstellung blaue Farbe.
Wie ändere ich diese Farben wie auf dem Bild unten?
Das Verhalten einiger Eigenschaften von UINavigationBar
wurde von iOS 7 geändert. Sie können im Bild unten sehen:
Zwei schöne Links, die ich mit Ihnen teilen möchte. Für weitere Informationen können Sie über diese Links gehen:
Apple Documentation für barTintColor sagt:
Diese Farbe ist standardmäßig durchscheinend, es sei denn, Sie setzen die durchscheinende Eigenschaft auf NO.
Beispielcode:
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[self.navigationController.navigationBar
setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
self.navigationController.navigationBar.translucent = NO;
Ich brauchte ungefähr einen halben Tag, um das herauszufinden, aber das hat bei mir funktioniert. In den rootViewController, der den navigationController initialisiert, füge ich diesen Code in meine viewDidAppear-Methode ein:
//set bar color
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:85.0/255.0 green:143.0/255.0 blue:220.0/255.0 alpha:1.0]];
//optional, i don't want my bar to be translucent
[self.navigationController.navigationBar setTranslucent:NO];
//set title and title color
[self.navigationItem setTitle:@"Title"];
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]];
//set back button color
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
//set back button arrow color
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
Mein vollständiger Beitrag dazu hier
Swift3, ios 1
Um die Farbe global zuzuweisen, gehen Sie in AppDelegate didFinishLaunchingWithOptions
wie folgt vor:
let textAttributes = [NSForegroundColorAttributeName:UIColor.white]
UINavigationBar.appearance().titleTextAttributes = textAttributes
Swift 4, iOS 11
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
UINavigationBar.appearance().titleTextAttributes = textAttributes
Vins Antwort funktionierte großartig für mich. Dies ist die gleiche Lösung für C # -Entwickler, die Xamarin.iOS/MonoTouch verwenden:
var navigationBar = NavigationController.NavigationBar; //or another reference
navigationBar.BarTintColor = UIColor.Blue;
navigationBar.TintColor = UIColor.White;
navigationBar.SetTitleTextAttributes(new UITextAttributes() { TextColor = UIColor.White });
navigationBar.Translucent = false;
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
Swift/iOS8
let textAttributes = NSMutableDictionary(capacity:1)
textAttributes.setObject(UIColor.whiteColor(), forKey: NSForegroundColorAttributeName)
navigationController?.navigationBar.titleTextAttributes = textAttributes
Verwenden Sie diesen Code, um die Farbe des Titels UINavigationBar
auf die richtige Weise zu ändern:
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]];
UITextAttributeTextColor
ist in der neuesten Version von iOS 7 veraltet. Verwenden Sie stattdessen NSForegroundColorAttributeName
.
Wenn Sie den Titel Textgröße und den Textfarbe ändern möchten, müssen Sie das NSDictionary titleTextAttributes für 2 davon ändern Objekte:
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:13.0],NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName,
nil];
Ich denke, die vorherigen Antworten sind korrekt. Dies ist eine andere Möglichkeit, dasselbe zu tun. Ich teile es hier mit anderen, nur für den Fall, dass es für jemanden nützlich wird. So können Sie die Text-/Titelfarbe für die Navigationsleiste in ios7 ändern:
UIColor *red = [UIColor colorWithRed:254.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0];
NSMutableDictionary *navBarTextAttributes = [NSMutableDictionary dictionaryWithCapacity:1];
[navBarTextAttributes setObject:red forKey:NSForegroundColorAttributeName ];
self.navigationController.navigationBar.titleTextAttributes = navBarTextAttributes;
Es scheint, dass Accessibility
Kontrollen in der iOS Settings
überschreiben so ziemlich alles, was Sie versuchen, farblich auf die Schaltflächen der Navigationsleiste anzuwenden. Stellen Sie sicher, dass Sie alle Einstellungen auf die Standardpositionen zurückgesetzt haben (Kontrast erhöhen, Fettschrift, Schaltflächenformen usw. auf Aus setzen), da sich sonst nichts ändert. Sobald ich es getan hatte, funktionierte der gesamte Farbänderungscode wie erwartet. Möglicherweise müssen Sie nicht alle deaktivieren, aber ich habe es nicht weiter verfolgt.