Wie ändere ich die Farbe der Navigationsleiste in iOS 7?
Grundsätzlich möchte ich so etwas wie die Twitter-Navigationsleiste erreichen (aktualisiertes Twitter für iOS7
also). Ich habe in eine Navigationsleiste auf einem view controller
eingebettet. Ich möchte nur die Farbe der Navigationsleiste in Hellblau ändern, zusammen mit der Utility-Leiste oben. Ich kann in meinem storyboard
keine Option finden.
Das Verhalten von tintColor
für Balken wurde in iOS 7.0 geändert. Es wirkt sich nicht mehr auf den Hintergrund der Leiste aus.
Aus der Dokumentation:
barTintColor Klassenreferenz
Die Farbtonfarbe, die auf den Hintergrund der Navigationsleiste angewendet werden soll.
@property(nonatomic, retain) UIColor *barTintColor
Diskussion
Diese Farbe ist standardmäßig durchscheinend, es sei denn, Sie setzen die durchscheinende Eigenschaft auf NO
.
Verfügbarkeit
Verfügbar in iOS 7.0 und höher.
deklariert in
INavigationBar.h
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
// iOS 7.0 or later
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO;
}else {
// iOS 6.1 or earlier
self.navigationController.navigationBar.tintColor = [UIColor redColor];
}
Wir können dies auch verwenden, um die iOS-Version zu überprüfen, wie in iOS 7 UI Transition Guide erwähnt
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// iOS 6.1 or earlier
self.navigationController.navigationBar.tintColor = [UIColor redColor];
} else {
// iOS 7.0 or later
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO;
}
EDIT Mit xib
Mit dem Interface Builder in Xcode ist es sehr einfach, das zu tun, was in der ursprünglichen Frage gefordert wurde - das Aussehen der alten Navigationsleiste von Twitter mit blauem Hintergrund und weißem Text.
Das sollte dir bringen, was du willst. Hier ist ein Screenshot, der es einfacher macht zu sehen, wo die Änderungen vorgenommen werden sollen.
Beachten Sie, dass durch Ändern nur des Balkenfarbtons die Textfarbe in der Navigationsleiste oder der Statusleiste nicht geändert wird. Der Stil muss ebenfalls geändert werden.
self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;
// *barTintColor* sets the background color
// *tintColor* sets the buttons color
In einer Navigations-basierten App können Sie den Code in AppDelegate einfügen. Ein ausführlicherer Code könnte sein:
// Navigation bar appearance (background and title)
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor titleColor], NSForegroundColorAttributeName, [UIFont fontWithName:@"FontNAme" size:titleSize], NSFontAttributeName, nil]];
[[UINavigationBar appearance] setTintColor:[UIColor barColor]];
// Navigation bar buttons appearance
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor textBarColor], NSForegroundColorAttributeName, shadowColor, NSShadowAttributeName, [UIFont fontWithName:@"FontName" size:titleSize], NSFontAttributeName, nil];
Stellen Sie in viewDidLoad
Folgendes ein:
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
Ändern Sie (blueColor
) in die gewünschte Farbe.
Für Swift die Farbe der Navigationsleiste ändern:
self.navigationController?.navigationBar.barTintColor = UIColor.red
titel Schriftart, Größe, Farbe ändern:
self.title = "title"
self.navigationController?.navigationBar.titleTextAttributes = [
NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.font : UIFont(name: "Futura", size: 30)!
]
Wenn Sie einen Hex-Code verwenden möchten, ist dies der beste Weg.
Definieren Sie dies zuerst oben in Ihrer Klasse:
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
Geben Sie dann in der "Anwendung didFinishLaunchingWithOptions" Folgendes ein:
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x00b0f0)];
Setzen Sie den Hex-Code anstelle von 00b0f0 ein.
In iOS 7 müssen Sie die Eigenschaft -barTintColor verwenden:
navController.navigationBar.barTintColor = [UIColor barColor];
Wenn Sie ios6 und ios7 unterstützen müssen, erhalten Sie dieses spezielle Hellblau in Ihrem IViewController:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:89/255.0f green:174/255.0f blue:235/255.0f alpha:1.0f];
self.navigationController.navigationBar.translucent = NO;
}else{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:89/255.0f green:174/255.0f blue:235/255.0f alpha:1.0f];
}
}
Es ist tatsächlich einfacher als die Antworten, die ich hier gesehen habe:
1) Just make sure you select the navigation bar on the Navigation control.
2) Select the color you want in the bar tint.
3) You have other options too, and/or individually on each view (just play with it).
Ich hoffe das hilft jemandem. Die Antworten, die ich sah, gefielen mir nicht. Ich möchte meinen Code so sauber wie möglich halten. Ich sage nicht, dass es falsch ist, es programmatisch zu tun, aber es gibt Leute wie mich ... das ist für euch.
Um den Code von Rajneesh071 zu vervollständigen, möchten Sie möglicherweise auch die Titelfarbe der Navigationsleiste (und ggf. die Schriftart) festlegen, da sich das Standardverhalten von iOS 6 auf 7 geändert hat:
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7)
{
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
self.navigationController.navigationBar.translucent = NO;
NSMutableDictionary *textAttributes = [[NSMutableDictionary alloc] initWithDictionary:mainNavController.navigationBar.titleTextAttributes];
[textAttributes setValue:[UIColor whiteColor] forKey:UITextAttributeTextColor];
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
}
else
{
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
Füge nur diesen Code in dein ViewContorller
ein oder in dein AppDelegate
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
//This is For iOS6
[self.navigationController.navigationBar setTintColor:[UIColor yellowColor]];
}
else
{
//This is For iOS7
[self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];
}
//You could place this code into viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor redColor];
//change the nav bar colour
self.navigationController.view.backgroundColor = [UIColor redColor];
//change the background colour
self.navigationController.navigationBar.translucent = NO;
}
//Or you can place it into viewDidAppear
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:(BOOL)animated];
self.navigationController.navigationBar.tintColor = [UIColor redColor];
//change the nav bar colour
self.navigationController.view.backgroundColor = [UIColor redColor];
//change the background colour
self.navigationController.navigationBar.translucent = NO;
}
In einer navigationsbasierten Anwendung können Sie die Farbe ändern
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
self.navigationController.navigationBar.translucent = NO;
} else {
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
}
#define _kisiOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
if (_kisiOS7)
{
[[UINavigationBar appearance] setBarTintColor:[UIcolor redcolor]];
}
else
{
[[UINavigationBar appearance] setBackgroundColor:[UIcolor blackcolor]];
[[UINavigationBar appearance] setTintColor:[UIcolor graycolor]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
}
Für Farbe:
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
Für Bild
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar_320X44.png"] forBarMetrics:UIBarMetricsDefault];
Diese Frage und diese Antworten sind hilfreich. Mit ihnen konnte ich meine gewünschte dunkelblaue navigationBar
Farbe mit weißem Titel und Schaltflächentext einstellen.
Ich musste aber auch die Uhr, den Träger, die Signalstärke usw. auf Weiß stellen. Schwarz kontrastierte einfach nicht genug mit dem Dunkelblau.
Möglicherweise habe ich diese Lösung in einer der vorherigen Antworten übersehen, aber ich konnte diese Änderung vornehmen, indem ich diese Zeile zu meiner obersten Ebene viewController
's viewDidLoad
hinzufügte:
[self.navigationController.navigationBar setBarStyle:UIStatusBarStyleLightContent];