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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19029833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:44:13  来源:igfitidea点击:

IOS 7 Navigation Bar text and arrow color

iphoneiosobjective-cuinavigationbarios7

提问by 1110

I want to set background for Navigation Bar to be blackand all colors inside it to be white.

我想将 Navigation Bar 的背景设置为black ,并将其中的所有颜色设置为white

So, I used this code :

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

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

But back button text color, arrowand bar buttonhave still default blue color.
How to change those colors like on image below?

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

enter image description here

在此处输入图片说明

回答by Bhavin

Behavior from some of the properties of UINavigationBarhas changed from iOS 7. You can see in the image shown below :

UINavigationBariOS 7 开始,某些属性的行为发生了变化。您可以在下图中看到:

enter image description here

在此处输入图片说明



Two beautiful links I'd like to share with you. For more details you can go through these links :

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

  1. iOS 7 UI Transition Guide.
  2. How to Update Your App for iOS 7.
  1. iOS 7 UI 过渡指南
  2. 如何为 iOS 7 更新您的应用程序


Apple Documentationfor barTintColorsays :

苹果文档barTintColor说:

This color is made translucent by default unless you set the translucent property to NO.

默认情况下,此颜色是半透明的,除非您将半透明属性设置为 NO。

Sample Code :

示例代码:

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

回答by John Riselvato

enter image description here

在此处输入图片说明

This one took me about half a day to figure out but this is what worked for me. Inside the rootViewController that initializes the navigationController, I put this code inside my viewDidAppear method:

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

My full post on this here

我在这里的完整帖子

回答by lifeisfoo

Swift3, ios 10

Swift3,iOS 10

To globally assign the color, in AppDelegate didFinishLaunchingWithOptions:

要全局分配颜色,请在 AppDelegate 中didFinishLaunchingWithOptions

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

Swift 4, iOS 11

斯威夫特 4,iOS 11

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

回答by benhorgen

Vin's answer worked great for me. Here is the same solution for C# developers using Xamarin.iOS/MonoTouch:

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;

回答by Abo3atef

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

回答by LondonGuy

Swift / iOS8

斯威夫特/iOS8

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

回答by aZtraL-EnForceR

To change color of UINavigationBartitle the correct way use this code:

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

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

UITextAttributeTextColoris deprecated in lastest ios 7 version. Use NSForegroundColorAttributeNameinstead.

UITextAttributeTextColor在最新的 ios 7 版本中已弃用。使用NSForegroundColorAttributeName来代替。

回答by OhadM

If you're looking to change the title text sizeand the text coloryou have to change the NSDictionary titleTextAttributes, for 2 of its objects:

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

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

回答by grepit

I think previous answers are correct , this is another way of doing the same thing. I am sharing it here with others just in case if it becomes useful for someone. This is how you can change the text/title color for the navbar in ios7:

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

回答by strangeluck

It seems that Accessibilitycontrols in the iOS Settingsoverride pretty much everything you try to do color-wise to the navigation bar buttons. Make sure you have all the settings to the default positions (set increase contrast, bold text, button shapes, etc to off) otherwise you won't see anything change. Once I did it, all the color change code started working as expected. You might not need to turn them all off, but I didn't pursue it further.

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