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

111*_*110 234 iphone objective-c uinavigationbar ios ios7

我想将导航栏的背景设置为黑色,其中的所有颜色都是白色.

所以,我使用了这段代码:

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

但是后退按钮文本颜色,箭头条形按钮仍然是默认的蓝色.
如何更改下面图像的颜色?

在此输入图像描述

Bha*_*vin 753

某些属性的行为已从iOS 7UINavigationBar更改.您可以在下面显示的图像中看到:

在此输入图像描述


我想与你分享两个美丽的链接.有关详细信息,您可以浏览以下链接:

  1. iOS 7 UI过渡指南.
  2. 如何更新iOS 7应用程序.

苹果文档barTintColor说:

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

  • 不要忘记您可以在appDelegate中更改整个应用程序的色调:[[UINavigationBar appearance] setTintColor:[UIColor blackColor]]; (4认同)
  • 我之前读过它,但这只改变了文字而不是箭头.它还是蓝色的:( (2认同)
  • @Chelsea:这条线怎么样?:`[self.navigationController.navigationBar setTitleTextAttributes:@ {[UIFont fontWithName:@"YOURFONT"size:14],NSFontAttributeName}];` (2认同)

Joh*_*ato 85

在此输入图像描述

这个花了我大约半天才搞清楚,但这对我有用.在初始化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]];
Run Code Online (Sandbox Code Playgroud)

我在这里的完整帖子

  • 谢谢,它工作:).但是在iOS 7中不推荐使用UITextAttributeTextColor,现在你应该使用NSForegroundColorAttributeName (3认同)

lif*_*foo 37

Swift3,ios 10

要在AppDelegate中全局分配颜色didFinishLaunchingWithOptions:

let textAttributes = [NSForegroundColorAttributeName:UIColor.white]
UINavigationBar.appearance().titleTextAttributes = textAttributes
Run Code Online (Sandbox Code Playgroud)

Swift 4,iOS 11

let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
UINavigationBar.appearance().titleTextAttributes = textAttributes
Run Code Online (Sandbox Code Playgroud)


ben*_*gen 21

Vin的回答对我很有帮助.以下是使用Xamarin.iOS/MonoTouch的C#开发人员的相同解决方案:

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


Abo*_*tef 12

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
Run Code Online (Sandbox Code Playgroud)


Lon*_*Guy 12

Swift/iOS8

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

  • 让textAttributes = NSMutableDictionary(capacity:1)现在是对NSMutableDictionary初始化的正确调用. (3认同)

aZt*_*ceR 8

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

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]];
Run Code Online (Sandbox Code Playgroud)

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


Oha*_*adM 5

如果您要更改标题文本大小文本颜色,则必须为其中的两个对象更改NSDictionary titleTextAttributes:

    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:13.0],NSFontAttributeName,
                                                                  [UIColor whiteColor], NSForegroundColorAttributeName, 
                                                                  nil];
Run Code Online (Sandbox Code Playgroud)