Objective C: 如何在导航栏中更改文本颜色

14

我通过以下代码更改了我的导航栏颜色

navconFvc.navigationBar.tintColor = [UIColor colorWithHexString:@"faf6f5"];

代码可以运行,但文本颜色也需要更改(请参见下面的截图)。右侧的刷新按钮标志也会受到影响。

enter image description here

如果我导航到堆栈中的另一页,将会出现相同的问题。

enter image description here

问题:在我改变导航栏的背景颜色后,如何更改以下内容的颜色:

  • 标题文本
  • 返回按钮文本和
  • 右侧栏按钮图标颜色?
4个回答

18

iOS 7中,只需使用:

self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};

使用您想要的任何文本颜色替换 [UIColor whiteColor]


15

1
Rincon-Fadul,谢谢提供链接,第一个链接对我很有效。然而,我仍不确定如何更改按钮的文本颜色或图标颜色。对于图标,我可能可以直接更改图像。但是对于后退按钮的文本颜色怎么办?该链接并没有真正解释这个部分。 - Zhen
旧答案,请使用@Erwan的答案。 - Zeb

7

改变文字颜色:

_navController.navigationBar.titleTextAttributes 
         = @{UITextAttributeTextColor : [UIColor blackColor]};

添加刷新按钮并将其着色:

UIBarButtonItem *button = [[UIBarButtonItem alloc]
         initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
         target:self action:@selector(reload)];

[button setTintColor:[UIColor blackColor]];
self.navigationItem.rightBarButtonItem = button;

影响导航栏背景的变量:

_navController.navigationBar.backgroundColor = [UIColor whiteColor];
_navController.navigationBar.tintColor = [UIColor whiteColor];
_navController.navigationBar.translucent = NO;

1
我刚刚编写了一个简单的UIViewController子类,它添加了一个可自定义的返回按钮,允许您更改文本颜色。它基本上添加了一些willAppear/willDisappear逻辑,以动画方式显示后退按钮,就像使用leftBarButtonItem属性时UINavigationController所做的那样。您还可以将其扩展为执行rightBarButtomItem

https://github.com/typeoneerror/BBCustomBackButtonViewController


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接