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

时间:2013-08-11 21:43:29

标签: ios ios6 interface-builder ios7

我想更改导航栏颜色的颜色,但我不确定是否应该更改色调或背景。我知道iOS 7的设计更加扁平(甚至是recommending removing gradients),但我无法解读这两个问题。即使我设置了背景颜色,也没有做任何事情。

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

Enter image description here

16 个答案:

答案 0 :(得分:107)

在iOS 7.0上,条形的tintColor的行为已更改。它不再影响条形图的背景,其行为与添加到UIView的tintColor属性的描述相同。 要为条形图的背景着色,请使用-barTintColor。

navController.navigationBar.barTintColor = [UIColor navigationColor];

答案 1 :(得分:79)

如果你想在 iOS 6 中为导航栏设置一个纯色,类似于iOS 7,请使用:

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

iOS 7 中的

使用barTintColor,如下所示:

navigationController.navigationBar.barTintColor = [UIColor greenColor];

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

答案 2 :(得分:36)

//在ios 7中: -

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

//在ios 6中: -

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

答案 3 :(得分:11)

UINavigationBar上忽略背景颜色属性,因此如果要调整外观,则必须使用tintColor或调用“自定义”下面列出的其他一些方法条形码外观“UINavigationBar class reference(如setBackgroundImage:forBarMetrics:)。

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

答案 4 :(得分:5)

还有一件事,如果你想改变 UIPopover 中的导航bg颜色,你需要将barStyle设置为UIBarStyleBlack

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

答案 5 :(得分:4)

以下是如何为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;
    }
}

答案 6 :(得分:4)

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

 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]];
}

答案 7 :(得分:4)

您可以查看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];
}

答案 8 :(得分:3)

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

/* 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];
}

答案 9 :(得分:2)

    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]];

答案 10 :(得分:2)

在AppDelegate.m中的didFinishLaunchingWithOptions()中插入以下代码

[[UINavigationBar appearance] setBarTintColor:[UIColor
    colorWithRed:26.0/255.0 green:184.0/255.0 blue:110.0/255.0 alpha:1.0]];

答案 11 :(得分:1)

我正在使用以下代码(在C#中)来更改NavigationBar的颜色:

NavigationController.NavigationBar.SetBackgroundImage (new UIImage (), UIBarMetrics.Default);
NavigationController.NavigationBar.SetBackgroundImage (new UIImage (), UIBarMetrics.LandscapePhone);
NavigationController.NavigationBar.BackgroundColor = UIColor.Green;

诀窍是你需要摆脱默认的背景图像然后才会出现颜色。

答案 12 :(得分:1)

如果要更改导航栏的颜色,请使用它的barTintColor属性。此外,如果您将任何颜色设置为tintColor,则会影响导航栏的项目,如按钮。

仅供参考,您希望保留iOS 6样式栏,使背景图像看起来像以前的样式并设置它。

有关详细信息,您可以从以下链接获取更多信息:

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/AppearanceCustomization.html

答案 13 :(得分:1)

在iOS7中,如果您的导航控制器包含在标签栏,splitview或其他一些容器中,那么对于全局更改导航栏外观,请使用以下方法::

[[UINavigationBar appearanceWhenContainedIn:[UITabBarController class],nil] setBarTintColor:[UIColor blueColor]];

答案 14 :(得分:0)

- (void)viewDidLoad

ViewController.m中尝试以下代码

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

这在iOS 6中对我有用..试试吧..

答案 15 :(得分:-4)

我不确定更改色调与背景颜色,但这是您更改导航栏的色调颜色的方式:

试试这段代码..

[navigationController.navigationBar setTintColor:[UIColor redColor]; //红色为例。