繁体   English   中英

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

[英]How to change Navigation Bar color in iOS 7?

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

基本上我想要实现类似Twitter Nav Bar(更新的iOS7推特)。 我嵌入了一个view controller顶部的导航栏。 我想要的是将导航栏颜色更改为浅蓝色以及顶部的实用工具栏。 我似乎无法在storyboard找到一个选项。

在iOS 7.0中,条形的tintColor的行为已更改。 它不再影响酒吧的背景。

从文档:

barTintColor 类参考

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

@property(nonatomic, retain) UIColor *barTintColor

讨论
默认情况下,此颜色为半透明,除非您将半透明属性设置为NO

可用性

适用于iOS 7.0及更高版本。

宣告进入
UINavigationBar.h

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 7 UI Transition Guide中,我们也可以使用它来检查iOS版本

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

在此输入图像描述

通过在Xcode中使用Interface Builder,可以很容易地完成原始问题 - 获取旧Twitter的Nav Bar外观,蓝色背景和白色文本。

  • 使用文档大纲,选择导航栏。
  • 在“属性”检查器的“导航栏”组中,将“样式”从“默认”更改为“黑色”。 这会将导航和状态栏的背景颜色更改为黑色,将其文本更改为白色。 因此,当应用程序运行时,状态栏中的电池和其他图标和文本将显示为白色。
  • 在同一导航栏组中,将条形色调更改为您喜欢的颜色。
  • 如果导航栏中有条形按钮项目,它们仍将以默认的蓝色显示其文本,因此在“属性”检查器的“视图”组中,将“色调”更改为“白色”。

这应该会得到你想要的。 这是一个屏幕截图,可以更容易地看到进行更改的位置。

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

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

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

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

在基于导航的应用程序中,您可以将代码放在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];

viewDidLoad ,设置:

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

将( blueColor )更改为您想要的任何颜色。

对于快速更改导航栏颜色:

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)!
    ]

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

首先,在您的班级顶部定义:

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

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

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

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

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

navController.navigationBar.barTintColor = [UIColor barColor];

如果您需要支持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];
    }
}

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

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

我希望这有助于某人。 我不喜欢我看到的答案。 我喜欢尽可能保持我的代码清洁。 并不是说以编程方式执行它是错误的,但有些人像我一样......这是给你们的。 更改导航栏的颜色

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

仅在ViewContorller 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]];
}
//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;
}

在基于导航的应用程序中,您可以更改颜色

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

这个问题和这些答案都很有帮助。 通过它们,我可以设置我想要的深蓝色navigationBar颜色,白色标题和按钮文字。

但我还需要将时钟,载波,信号强度等改为白色。 黑色与深蓝色的对比度不够。

我可能在前面的一个答案中忽略了这个解决方案,但我能够通过将此行添加到我的顶级viewControllerviewDidLoad来进行更改:

[self.navigationController.navigationBar setBarStyle:UIStatusBarStyleLightContent];
- (void)viewDidLoad
{
    [super viewDidLoad];

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

对于颜色:

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

对于图像

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar_320X44.png"] forBarMetrics:UIBarMetricsDefault];

暂无
暂无

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

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