IOS 7导航栏文本和箭头颜色

我想设置导航栏的背景是黑色的 ,里面的所有颜色都是白色的

所以,我用这个代码:

[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], NSForegroundColorAttributeName, [UIFont fontWithName:@"Arial-Bold" size:0.0], NSFontAttributeName, nil]]; 

但后退button的文字颜色箭头酒吧button仍然是默认的蓝色
如何改变这些颜色,如下面的图片?

在这里输入图像描述

来自iOS 7的一些UINavigationBar属性的行为已经改变。 你可以看到如下图所示:

在这里输入图像描述


两个美丽的链接,我想与你分享。 欲了解更多详情,你可以通过这些链接:

  1. iOS 7 UI转换指南 。
  2. 如何更新您的应用程序的iOS 7 。

barTintColor的 Apple文档说:

除非将半透明属性设置为NO,否则此颜色默认为半透明。

示例代码:

 self.navigationController.navigationBar.barTintColor = [UIColor blackColor]; self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; self.navigationController.navigationBar.translucent = NO; 

在这里输入图像描述

这一天花了我半天左右的时间才搞清楚,但是这对我是有效的。 在初始化navigationController的rootViewController里面,我把这段代码放在我的viewDidAppear方法里面:

 //set bar color [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:85.0/255.0 green:143.0/255.0 blue:220.0/255.0 alpha:1.0]]; //optional, i don't want my bar to be translucent [self.navigationController.navigationBar setTranslucent:NO]; //set title and title color [self.navigationItem setTitle:@"Title"]; [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]]; //set back button color [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; //set back button arrow color [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]]; 

我在这里完整的post

Swift3,ios 10

要在AppDelegate didFinishLaunchingWithOptions全局分配颜色,请执行以下操作:

 let textAttributes = [NSForegroundColorAttributeName:UIColor.white] UINavigationBar.appearance().titleTextAttributes = textAttributes 

Swift 4,iOS 11

 let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white] UINavigationBar.appearance().titleTextAttributes = textAttributes 

Vin的回答对我很好。 下面是使用Xamarin.iOS / MonoTouch的C#开发人员的相同解决scheme:

 var navigationBar = NavigationController.NavigationBar; //or another reference navigationBar.BarTintColor = UIColor.Blue; navigationBar.TintColor = UIColor.White; navigationBar.SetTitleTextAttributes(new UITextAttributes() { TextColor = UIColor.White }); navigationBar.Translucent = false; 
 [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; 

Swift / iOS8

 let textAttributes = NSMutableDictionary(capacity:1) textAttributes.setObject(UIColor.whiteColor(), forKey: NSForegroundColorAttributeName) navigationController?.navigationBar.titleTextAttributes = textAttributes 

要更改UINavigationBar标题的颜色,请使用以下代码:

 [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]]; 

UITextAttributeTextColor在最新的ios 7版本中已弃用。 改用NSForegroundColorAttributeName

如果你想改变标题的文本大小文本颜色,你必须改变NSDictionary的titleTextAttributes ,对于它的两个对象:

  self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:13.0],NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, nil]; 

我认为以前的答案是正确的,这是做同样的事情的另一种方式。 我在这里与他人分享,以防万一某人变得有用。 这是你可以如何更改ios7中的导航栏的文本/标题颜色:

 UIColor *red = [UIColor colorWithRed:254.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0]; NSMutableDictionary *navBarTextAttributes = [NSMutableDictionary dictionaryWithCapacity:1]; [navBarTextAttributes setObject:red forKey:NSForegroundColorAttributeName ]; self.navigationController.navigationBar.titleTextAttributes = navBarTextAttributes; 

看起来, iOS Settings中的Accessibility控件几乎覆盖了您尝试对导航栏button进行色彩处理的所有内容。 确保将所有设置都设置为默认位置(设置增加对比度, 粗体文本 ,button形状等),否则您将看不到任何更改。 一旦我做到了,所有的颜色变化代码就像预期的那样开始工作。 你可能不需要把它们全部关掉,但我没有进一步追求。