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

Pat*_*rra 208 uinavigationbar storyboard uikit ios ios7

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

基本上我想要实现像Twitter Nav Bar这样的东西(更新的Twitter iOS7就是这样).我嵌入了一个导航栏view controller.我想要的是将导航栏颜色更改为浅蓝色以及顶部的实用工具栏.我似乎找不到我的选择storyboard.

Raj*_*071 316

tintColor在iOS 7.0中,for栏的行为已发生变化.它不再影响酒吧的背景.

从文档:

barTintColor 类参考

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

@property(nonatomic, retain) UIColor *barTintColor
Run Code Online (Sandbox Code Playgroud)

讨论
默认情况下,此颜色为半透明,除非您将半透明属性设置为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];
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
Run Code Online (Sandbox Code Playgroud)

编辑 使用xib

在此输入图像描述

  • 但是我可以在哪里放置此代码?我是非常新的哈哈.我将它放在app委托中,它似乎并没有做太多.它建立得很好. (3认同)
  • if(floor(NSFoundationVersionNumber)<= NSFoundationVersionNumber_iOS_6_1)这样可以更好地测试您正在运行的iOS版本,如过渡指南中的Apple所述:https://developer.apple.com/library/ios/documentation/ userexperience /概念性/ TransitionGuide/SupportingEarlieriOS.html (2认同)

alo*_*ono 126

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

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

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

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

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

  • @ Rob85我已经检查了Apple的开发者网站,iOS 9中仍然提供样式`Default`和`Black`.不推荐使用的是'BlackOpaque`和`BlackTranslucent`,它们被'Black`风格和半透明的复选框.[Apple开发者网站上的UIBarStyle](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitDataTypesReference/index.html#//apple_ref/c/tdef/UIBarStyle) (2认同)

pNr*_*Nre 73

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

// *barTintColor* sets the background color
// *tintColor* sets the buttons color
Run Code Online (Sandbox Code Playgroud)

  • 我在哪里可以把这段代码?抱歉新手问题哈哈 (4认同)
  • 代码可以放在导航控制器的-(void)viewDidLoad方法中 (2认同)
  • 在iOS 9中,将barTintColor设置为黑色会使时间,设备名称和电池也变为黑色,因此您将看不到它们。为了避免这种情况,请使用self.navigationController.navigationBar.barStyle = UIBarStyleBlack;而不是使用barTintColor; (2认同)

RFG*_*RFG 44

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

  • 添加`[[UINavigationBar外观] setBarTintColor:[UIColor barColor]];`for iOS 7 (6认同)

小智 23

viewDidLoad,设置:

    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
Run Code Online (Sandbox Code Playgroud)

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


Wil*_* Hu 16

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

self.navigationController?.navigationBar.barTintColor = UIColor.red
Run Code Online (Sandbox Code Playgroud)

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

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


RFG*_*RFG 12

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

navController.navigationBar.barTintColor = [UIColor barColor];
Run Code Online (Sandbox Code Playgroud)


Kyl*_*law 12

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

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

#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]
Run Code Online (Sandbox Code Playgroud)

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

[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x00b0f0)];
Run Code Online (Sandbox Code Playgroud)

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


bba*_*iee 11

如果您需要支持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];
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在iOS 7上:-self.navigationController.navigationBar.barTintColor是条形颜色AND -self.navigationController.navigationBar.tintColor是按钮文本颜色(后退,完成...). (3认同)

pqs*_*qsk 9

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

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).
Run Code Online (Sandbox Code Playgroud)

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


小智 5

//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;
}
Run Code Online (Sandbox Code Playgroud)


Cod*_*rew 5

为了使 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];
}
Run Code Online (Sandbox Code Playgroud)


Dar*_*iya 5

仅在您的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]];
}
Run Code Online (Sandbox Code Playgroud)