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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18929864/
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 01:30:03  来源:igfitidea点击:

How to change Navigation Bar color in iOS 7?

iosios7storyboarduikituinavigationbar

提问by Patricio Guerra

How do I change the Navigation Bar color in iOS 7?

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

Basically I want to achieve something like the Twitter Nav Bar (updated Twitter for iOS7that is). I embedded-in a nav bar atop a view controller. All I want is to change the nav bar color to light blue along with the utility bar at the top. I can't seem to find an option in my storyboard.

基本上我想实现类似 Twitter Nav Bar 的东西(为此更新了 Twitter iOS7)。我嵌入了一个导航栏顶部的view controller. 我想要的只是将导航栏颜色与顶部的实用程序栏一起更改为浅蓝色。我似乎无法在我的storyboard.

回答by Rajneesh071

The behavior of tintColorfor bars has changed in iOS 7.0. It no longer affects the bar's background.

tintColorfor bar的行为在 iOS 7.0 中发生了变化。它不再影响酒吧的背景。

From the documentation:

从文档:

barTintColorClass Reference

barTintColor类参考

The tint color to apply to the navigation bar background.

应用于导航栏背景的色调颜色。

@property(nonatomic, retain) UIColor *barTintColor

Discussion
This color is made translucent by default unless you set the translucent property to NO.

讨论
默认情况下,这种颜色是半透明的,除非您将 translucent 属性设置为NO

Availability

可用性

Available in iOS 7.0 and later.

在 iOS 7.0 及更高版本中可用。

Declared In
UINavigationBar.h

UINavigationBar.h 中声明

Code

代码

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

We can also use this to check iOS Version as mention in iOS 7 UI Transition Guide

我们也可以使用它来检查 iOS 版本,如 iOS 7 UI Transition Guide 中所述

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

EDITUsing xib

使用xib 编辑

enter image description here

在此处输入图片说明

回答by alondono

Doing what the original question asked—to get the old Twitter's Nav Bar look, blue background with white text—is very easy to do just using the Interface Builder in Xcode.

做最初的问题——获得旧的 Twitter 导航栏外观,蓝色背景和白色文本——只需使用 Xcode 中的界面构建器就很容易做到。

  • Using the Document Outline, select your Navigation Bar.
  • In the Attributes Inspector, in the Navigation Bar group, change the Style from Default to Black. This changes the background colour of the Navigation and Status bars to black, and their text to white. So the battery and other icons and text in the status bar will look white when the app is running.
  • In the same Navigation Bar group, change the Bar Tint to the colour of your liking.
  • If you have Bar Button items in your Navigation Bar, those will still show their text in the default blue colour, so in the Attributes Inspector, View group, change the Tint to White Colour.
  • 使用文档大纲,选择您的导航栏。
  • 在属性检查器的导航栏组中,将样式从默认更改为黑色。这会将导航栏和状态栏的背景颜色更改为黑色,并将其文本更改为白色。因此,当应用程序运行时,状态栏中的电池和其他图标和文本将显示为白色。
  • 在同一个导航栏组中,将 Bar Tint 更改为您喜欢的颜色。
  • 如果导航栏中有条形按钮项,它们仍会以默认的蓝色显示其文本,因此在属性检查器的视图组中,将色调更改为白色。

That should get you what you want. Here is a screenshot that would make it easier to see where to make the changes.

那应该让你得到你想要的。这是一个屏幕截图,可以更轻松地查看进行更改的位置。

Where to make the necessary changes, including the result in iOS Simulator

在哪里进行必要的更改,包括 iOS 模拟器中的结果

Note that changing only the Bar Tint doesn't change the text colour in the Navigation Bar or the Status Bar. The Style also needs to be changed.

请注意,仅更改 Bar Tint 不会更改导航栏或状态栏中的文本颜色。风格也需要改变。

回答by pNre

self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;

// *barTintColor* sets the background color
// *tintColor* sets the buttons color

回答by RFG

In a Navigation based app you can put the code in AppDelegate. A more detailed code could be:

在基于导航的应用程序中,您可以将代码放在 AppDelegate 中。更详细的代码可能是:

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

回答by zeezoo

In viewDidLoad, set:

viewDidLoad设置:

    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];

Change ( blueColor) to whatever color you'd like.

将 ( blueColor)更改为您喜欢的任何颜色。

回答by William Hu

For swift change navigation bar color:

快速更改导航栏颜色:

self.navigationController?.navigationBar.barTintColor = UIColor.red

change title font,size, color:

更改标题字体、大小、颜色:

self.title = "title"
self.navigationController?.navigationBar.titleTextAttributes = [
        NSAttributedString.Key.foregroundColor : UIColor.white,
        NSAttributedString.Key.font : UIFont(name: "Futura", size: 30)!
    ]

回答by RFG

In iOS 7 you must use the -barTintColor property:

在 iOS 7 中,您必须使用 -barTintColor 属性:

navController.navigationBar.barTintColor = [UIColor barColor];

回答by Kyle Greenlaw

If you want to use a hex code, here is the best way to do so.

如果您想使用十六进制代码,这是最好的方法。

First, define this at the top of your class:

首先,在类的顶部定义它:

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

Then inside the "application didFinishLaunchingWithOptions", put this:

然后在“应用程序 didFinishLaunchingWithOptions”中,输入:

[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x00b0f0)];

Put you hex code in place of the 00b0f0.

用十六进制代码代替 00b0f0。

回答by bbaassssiiee

If you need to support ios6 and ios7 then you get that particular light blue using this in your UIViewController:

如果您需要支持 ios6 和 ios7,那么您可以在UIViewController 中使用它来获得特定的浅蓝色:

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

回答by pqsk

It's actually easier than the answers I've seen here:

它实际上比我在这里看到的答案更容易:

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).

I hope this helps somebody. I didn't like the answers I saw. I like to keep my code as clean as possible. Not saying that it's wrong to do it programmatically, but there are people out there like me....this is for you guys. Changing color of the navigation bar

我希望这对某人有帮助。我不喜欢我看到的答案。我喜欢让我的代码尽可能干净。并不是说以编程方式进行操作是错误的,但是有人像我一样......这是给你们的。 更改导航栏的颜色