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

时间:2013-09-26 13:35:05

标签: iphone ios objective-c uinavigationbar 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]];

但后退按钮文字颜色箭头栏按钮仍有默认蓝色
如何更改下面图片中的颜色?

enter image description here

11 个答案:

答案 0 :(得分:750)

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

enter image description here


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

  1. iOS 7 UI Transition Guide
  2. How to Update Your App for iOS 7

  3. 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;
    

答案 1 :(得分:85)

enter image description here

这个花了我大约半天的时间来弄明白,但这对我有用。 在初始化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]];

关于此here

的完整帖子

答案 2 :(得分:37)

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

答案 3 :(得分:20)

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;

答案 4 :(得分:12)

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

答案 5 :(得分:11)

Swift / iOS8

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

答案 6 :(得分:8)

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

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]];
最新的ios 7版本中不推荐使用

UITextAttributeTextColor。请改用NSForegroundColorAttributeName

答案 7 :(得分:5)

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

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

答案 8 :(得分:2)

我认为以前的答案是正确的,这是做同样事情的另一种方式。我在这里与其他人分享,以防它对某人有用。这是您可以在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;

答案 9 :(得分:1)

似乎Accessibility中的iOS Settings控件几乎覆盖了您尝试对导航栏按钮执行颜色操作的所有操作。确保将所有设置都设置为默认位置(设置增加对比度,粗体文字,按钮形状等关闭),否则您将看不到任何更改。完成后,所有颜色更改代码都按预期开始工作。你可能不需要全力以赴,但我没有进一步追求它。

答案 10 :(得分:0)

Swift 5 / iOS 13

要更改控制器中标题的颜色,请执行以下操作:

UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]