如何在 iOS 7 或 6 中更改导航栏颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18177010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:41:03  来源:igfitidea点击:

How to change navigation bar color in iOS 7 or 6?

iosios6interface-builderios7

提问by EGHDK

I want to change the color of the navigation bar color, but I'm not sure whether or not I should change the tint or the background. I know iOS 7 is going for a more flat design (even recommending removing gradients), but I am having trouble deciphering the two. Even if I set a background color, it doesn't do anything.

我想更改导航栏颜色的颜色,但我不确定是否应该更改色调或背景。我知道 iOS 7 将采用更扁平的设计(甚至建议删除渐变),但我无法解读这两者。即使我设置了背景颜色,它也没有任何作用。

In this image, the background is set to green, but the bar is still blue:

在此图像中,背景设置为绿色,但条形图仍为蓝色:

Enter image description here

在此处输入图片说明

回答by Mahesh

The behavior of tintColor for bars has changed on iOS 7.0. It no longer affects the bar's background and behaves as described for the tintColor property added to UIView. To tint the bar's background, please use -barTintColor.

条形的 tintColor 行为在 iOS 7.0 上发生了变化。它不再影响栏的背景,其行为与添加到 UIView 的 tintColor 属性相同。要为条形图的背景着色,请使用 -barTintColor。

navController.navigationBar.barTintColor = [UIColor navigationColor];

navController.navigationBar.barTintColor = [UIColor navigationColor];

回答by carmen_munich

If you want to have a solid color for your navigation bar in iOS 6similar to iOS 7 use this:

如果您想在类似于 iOS 7 的iOS 6 中为导航栏设置纯色,请使用以下命令:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor greenColor]];


in iOS 7use the barTintColorlike this:

iOS 7 中使用barTintColor这样的:

navigationController.navigationBar.barTintColor = [UIColor greenColor];

or

或者

 [[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];

回答by Ashish

// In ios 7 :-

// 在 ios 7 中:-

[self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];

// In ios 6 :-

// 在 ios 6 中:-

[self.navigationController.navigationBar setTintColor:[UIColor yellowColor]];

回答by Charles A.

The background color property is ignored on a UINavigationBar, so if you want to adjust the look and feel you either have to use the tintColoror call some of the other methods listed under "Customizing the Bar Appearance" of the UINavigationBar class reference(like setBackgroundImage:forBarMetrics:).

上的背景颜色属性被忽略UINavigationBar,因此如果您想调整外观,您必须使用tintColor或调用UINavigationBar 类参考(如setBackgroundImage:forBarMetrics:)的“自定义栏外观”下列出的一些其他方法。

Be aware that the tintColorproperty works differently in iOS 7, so if you want a consistent look between iOS 7 and prior version using a background image might be your best bet. It's also worth mentioning that you can't configure the background image in the Storyboard, you'll have to create an IBOutletto your UINavigationBarand change it in viewDidLoador some other appropriate place.

请注意,该tintColor属性在 iOS 7 中的工作方式不同,因此如果您希望使用背景图像在 iOS 7 和之前版本之间保持一致的外观可能是您的最佳选择。还值得一提的是,您无法在 Storyboard 中配置背景图像,您必须IBOutlet为您创建一个UINavigationBar并在viewDidLoad或其他适当的地方更改它。

回答by Tarek Hallak

One more thing, if you want to change the navigation bg color in UIPopoveryou need to set barStyleto UIBarStyleBlack

还有一件事,如果你想改变UIPopover 中的导航背景颜色,你需要设置barStyleUIBarStyleBlack

if([UINavigationBar instancesRespondToSelector:@selector(barTintColor)]){ //iOS7
    navigationController.navigationBar.barStyle = UIBarStyleBlack;
    navigationController.navigationBar.barTintColor = [UIColor redColor];
}

回答by kalan nawarathne

The complete code with version checking.

带有版本检查的完整代码。

 if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {

    // do stuff for iOS 7 and newer
    [self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];
}
else {

    // do stuff for older versions than iOS 7
    [self.navigationController.navigationBar setTintColor:[UIColor yellowColor]];
}

回答by PRITAM SATPUTE

You can check iOS Version and simply set the tint color of Navigation bar.

您可以检查 iOS 版本并简单地设置导航栏的色调颜色。

if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.9529 green:0.4392 blue:0.3333 alpha:1.0];
}else{

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.9529 green:0.4392 blue:0.3333 alpha:1.0];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

回答by pizzamonster

Here is how to set it correctly for both iOS 6 and 7.

以下是如何为 iOS 6 和 7 正确设置它。

+ (void)fixNavBarColor:(UINavigationBar*)bar {
    if (iosVersion >= 7) {
        bar.barTintColor = [UIColor redColor];
        bar.translucent = NO;
    }else {
        bar.tintColor = [UIColor redColor];
        bar.opaque = YES;
    }
}

回答by Roozbeh Zabihollahi

Based on posted answered, this worked for me:

根据发布的回答,这对我有用:

/* check for iOS 6 or 7 */
if ([[self navigationController].navigationBar respondsToSelector:@selector(setBarTintColor:)]) {
    [[self navigationController].navigationBar setBarTintColor:[UIColor whiteColor]];

} else {
    /* Set background and foreground */
    [[self navigationController].navigationBar setTintColor:[UIColor whiteColor]];
    [self navigationController].navigationBar.titleTextAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:[UIColor blackColor],UITextAttributeTextColor,nil];
}

回答by Hitesh Vaghela

    you can add bellow code in appdelegate.m .if your app is navigation based

    // for background color
   [nav.navigationBar setBarTintColor:[UIColor blueColor]];

    // for change navigation title and button color
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],
    NSForegroundColorAttributeName,               
    [UIFont fontWithName:@"FontNAme" size:20],
    NSFontAttributeName, nil]];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];