我如何改变导航栏的颜色在iOS 7?
基本上我想要实现类似Twitter导航栏的东西(更新的Twitter为iOS7)。我在视图控制器的顶部嵌入了一个导航条。所有我想要的是改变导航栏的颜色为浅蓝色连同顶部的工具栏。我似乎在我的故事板中找不到一个选项。
我如何改变导航栏的颜色在iOS 7?
基本上我想要实现类似Twitter导航栏的东西(更新的Twitter为iOS7)。我在视图控制器的顶部嵌入了一个导航条。所有我想要的是改变导航栏的颜色为浅蓝色连同顶部的工具栏。我似乎在我的故事板中找不到一个选项。
self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;
// *barTintColor* sets the background color
// *tintColor* sets the button's color
在ios7中,你必须使用-barTintColor属性:
navController.navigationBar.barTintColor = [UIColor barColor];
tintColor对条形图的行为在ios7.0中发生了改变。它不再影响酒吧的背景。
从文档中可以看到:
barTintColor类引用
要应用于导航栏背景的色调。
@property(nonatomic, retain) UIColor *barTintColor
讨论 默认情况下,这种颜色是半透明的,除非你将半透明属性设置为NO。
可用性
支持iOS 7.0及以上版本。
中声明 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];
}
我们也可以使用这个来检查iOS版本,正如iOS 7 UI过渡指南中提到的那样
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;
}
编辑 使用xib
在一个基于导航的应用程序中,你可以把代码放在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];
如果你需要支持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];
}
}
在viewDidLoad中,设置:
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
更改(blueColor)为任何你想要的颜色。
//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;
}
为了使Rajneesh071的代码完整,你可能还想设置导航栏的标题颜色(和字体,如果你想要的话),因为默认行为从iOS 6改变到7:
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];
}
在基于导航的应用程序中,您可以更改颜色
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 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]
然后在“application didFinishLaunchingWithOptions”里面,放这个:
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x00b0f0)];
把十六进制代码放在00b0f0的位置。
这个问题和这些答案很有帮助。有了它们,我可以设置我想要的深蓝色导航栏颜色,白色标题和按钮文本。
但我还需要将时钟、载波、信号强度等更改为白色。黑色和深蓝色的对比不够明显。
我可能在之前的回答中忽略了这个解决方案,但我能够通过在我的顶级viewController的viewDidLoad中添加这一行来进行更改:
[self.navigationController.navigationBar setBarStyle:UIStatusBarStyleLightContent];
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
}
#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]];
}
它实际上比我在这里看到的答案更简单:
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).
我希望这能帮助到一些人。我不喜欢我看到的答案。我喜欢让我的代码尽可能的干净。并不是说用编程的方式做这件事是错误的,但确实有像我这样的人....这是给你们的。
在viewcontroller或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]];
}
做最初的问题——得到旧的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.
这应该能让你得到你想要的。下面是一个截图,可以让你更容易地看到在哪里进行更改。
注意,只改变条形颜色不会改变导航栏或状态栏的文本颜色。样式也需要改变。
颜色:
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
的图像
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar_320X44.png"] forBarMetrics:UIBarMetricsDefault];
快速改变导航条颜色:
self.navigationController?.navigationBar.barTintColor = UIColor.red
更改标题字体、大小、颜色:
self.title = "title"
self.navigationController?.navigationBar.titleTextAttributes = [
NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.font : UIFont(name: "Futura", size: 30)!
]