ChatGPT解决这个技术问题 Extra ChatGPT

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

但是后退按钮文本颜色、箭头和条形按钮仍然具有默认的蓝色。如何更改这些颜色,如下图所示?

https://i.stack.imgur.com/xwOa0.jpg

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

A
Alex Cio

UINavigationBar 的某些属性的行为与 iOS 7 不同。您可以在下图中看到:

https://i.stack.imgur.com/DtNbN.png

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

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

barTintColorApple 文档 说:

除非您将 translucent 属性设置为 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]];
J
John Riselvato

https://i.stack.imgur.com/bCv2y.png

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


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

Swift3,iOS 10

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

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

斯威夫特 4,iOS 11

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

b
benhorgen

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。
R
RameshVel

斯威夫特/iOS8

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

let textAttributes = NSMutableDictionary(capacity:1) 现在是对 NSMutableDictionary 初始化的正确调用。
S
Sebyddd
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

我喜欢这种方法,因为您可以在 AppDelegate 本身中设置它,并且它使用 Appearance 代理。
R
Ravi Gautam

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

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

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


O
OhadM

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

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

g
grepit

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

P
Patrick

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


正是发生在我身上。当“粗体文本”打开时,我无法更改条形按钮项的色调颜色。这是不好的。现在我将不得不使用图形来代替:-(
M
Mahdi Moqadasi

斯威夫特 5/iOS 13

要更改控制器中标题的颜色:

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