简体   繁体   English

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

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

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. 我知道iOS 7将采用更平面的设计(甚至建议删除渐变 ),但我无法破译这两个。 Even if I set a background color, it doesn't do anything. 即使我设置了背景颜色,也没有做任何事情。

In this image, the background is set to green, but the bar is still blue: 在此图像中,背景设置为绿色,但条形仍为蓝色:

在此输入图像描述

The behavior of tintColor for bars has changed on iOS 7.0. 在iOS 7.0上,tintColor对条形的行为已经改变。 It no longer affects the bar's background and behaves as described for the tintColor property added to UIView. 它不再影响条形图的背景,其行为与添加到UIView的tintColor属性的描述相同。 To tint the bar's background, please use -barTintColor. 要为条形图的背景着色,请使用-barTintColor。

navController.navigationBar.barTintColor = [UIColor navigationColor];

If you want to have a solid color for your navigation bar in iOS 6 similar to iOS 7 use this: 如果你想在iOS 6中使用类似于iOS 7的导航栏的纯色,请使用:

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

in iOS 7 use the barTintColor like this: iOS 7中使用barTintColor如下:

navigationController.navigationBar.barTintColor = [UIColor greenColor];

or 要么

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

// In ios 7 :- //在ios 7中: -

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

// In ios 6 :- //在ios 6中: -

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

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 tintColor or 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 tintColor property 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. 请注意, tintColor属性在iOS 7中的工作方式不同,因此如果您希望使用背景图像在iOS 7和先前版本之间保持一致的外观可能是您最好的选择。 It's also worth mentioning that you can't configure the background image in the Storyboard, you'll have to create an IBOutlet to your UINavigationBar and change it in viewDidLoad or some other appropriate place. 还值得一提的是,您无法在Storyboard中配置背景图像,您必须为UINavigationBar创建一个IBOutlet并在viewDidLoad或其他适当的位置更改它。

One more thing, if you want to change the navigation bg color in UIPopover you need to set barStyle to UIBarStyleBlack 还有一件事,如果你想在UIPopover中更改导航bg颜色,你需要将barStyle设置为UIBarStyleBlack

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

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

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

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

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

Insert the below code in didFinishLaunchingWithOptions() in AppDelegate.m 在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]];

I'm using following code (in C#) to change the color of the NavigationBar: 我正在使用以下代码(在C#中)来更改NavigationBar的颜色:

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

The trick is that you need to get rid of the default background image and then the color will appear. 诀窍是你需要摆脱默认的背景图像,然后会出现颜色。

If you want to change a color of a navigation bar, use barTintColor property of it. 如果要更改导航栏的颜色,请使用它的barTintColor属性。 In addition, if you set any color to tintColor of it, that affects to the navigation bar's item like a button. 此外,如果您将任何颜色设置为tintColor ,则会影响导航栏的项目,如按钮。

FYI, you want to keep iOS 6 style bar, make a background image looks like previous style and set it. 仅供参考,你想保留iOS 6风格的条形图,使背景图像看起来像以前的风格并设置它。

For more detail, you can get more information from the following link: 有关更多详细信息,您可以从以下链接获取更多信息:

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

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

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

Try the code below in the - (void)viewDidLoad of your ViewController.m ViewController.m- (void)viewDidLoad中尝试以下代码

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

this did work for me in iOS 6.. Try it.. 这在iOS 6中对我有用..试试吧..

I'm not sure about changing the tint vs the background color but this is how you change the tint color of the Navigation Bar: 我不确定改变色调与背景颜色,但这是你改变导航栏的色调颜色的方法:

Try this code.. 试试这个代码..

[navigationController.navigationBar setTintColor:[UIColor redColor]; //Red as an example. //红色为例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM