【问题标题】:iOS 7 Navigation Bar text and arrow coloriOS 7 导航栏文本和箭头颜色
【发布时间】:2013-10-02 12:22:36
【问题描述】:

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

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

[[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]];

但是后退按钮文字颜色箭头条形按钮仍然有默认的蓝色
如何更改如下图所示的颜色?

【问题讨论】:

  • 在 AppDelegate 中尝试 if ([self.window respondsToSelector:@selector(setTintColor:)]) self.window.tintColor = [UIColor whiteColor];
  • 有趣的是,您的代码对我来说效果很好(只是想将文本颜色更改为白色)。谢谢!
  • 您使用的蓝色是什么颜色?看起来不错!
  • @ebi 有些人不喜欢分享。只需使用颜色选择器来挑选它。或者....你去:RGB:(18、60、133)

标签: iphone ios objective-c uinavigationbar ios7


【解决方案1】:

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


我想与您分享两个漂亮的链接。有关更多详细信息,您可以通过这些链接:

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

Apple 文档 for 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;

【讨论】:

  • 我试过了:self.nav.navigationBar.tintColor = [UIColor whiteColor]; self.nav.navigationBar.barTintColor = [UIColor colorWithRed:6.0/255.0 green:12.0/255.0 blue:19.0/255.0 alpha:1.0];但一切仍然是蓝色的。
  • 我以前读过它,但这只改变了文本而不是箭头。它仍然是蓝色的:(
  • @1110:看看:Using Tint Color
  • @Chelsea:这条线怎么样? :[self.navigationController.navigationBar setTitleTextAttributes:@{[UIFont fontWithName:@"YOURFONT" size:14], NSFontAttributeName}];
  • 别忘了你可以在 appDelegate 中改变整个应用的色调:[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
【解决方案2】:

我花了大约半天的时间才弄明白,但这对我有用。 在初始化 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 上的完整帖子

【讨论】:

  • 如果您在更改后退箭头的颜色时遇到问题,这个 sn-p 将解决问题。至少,您需要将最后一行代码添加到实际的 rootViewController。
  • 是的,约翰。我昨天也浪费了大约一个小时。一个问题(大问题)是文档在指出所有可能的属性方面很糟糕。知道它们可能在哪里列出吗?泰。
  • 谢谢@john 它与我的自定义颜色重叠,你能解释一下为什么吗?
  • 我认为您应该创建一个新问题。除非我看到你的代码和你在做什么,否则我无法告诉你为什么。 @MacGeek
  • 谢谢,它成功了:)。但是 UITextAttributeTextColor 在 iOS 7 中被弃用了,现在你应该使用 NSForegroundColorAttributeName
【解决方案3】:

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;

【讨论】:

  • 不知道您可以按照您的方式设置 SetTitleTextAttributes。
【解决方案4】:

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

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

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

【讨论】:

    【解决方案5】:

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

    【讨论】:

      【解决方案6】:
      [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
      

      【讨论】:

      • 我喜欢这种方法,因为您可以在 AppDelegate 本身中设置它,它使用外观代理。
      【解决方案7】:

      Swift / iOS8

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

      【讨论】:

      • 让 textAttributes = NSMutableDictionary(capacity:1) 现在是对 NSMutableDictionary 初始化的正确调用。
      【解决方案8】:

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

      【讨论】:

      • 正是发生在我身上的事情。当“粗体文本”打开时,我无法更改条形按钮项的色调颜色。这是不好的。现在我将不得不使用图形来代替:-(
      【解决方案9】:

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

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

      【讨论】:

        【解决方案10】:

        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
        

        【讨论】:

          【解决方案11】:

          Swift 5/iOS 13

          在控制器中改变标题的颜色:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-09-26
            • 2013-08-25
            相关资源
            最近更新 更多