ios 目标 C:如何更改导航栏中的文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6805025/
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-30 20:51:16  来源:igfitidea点击:

Objective C: How to change text color in navigation bar

objective-ciosiconsuinavigationbartextcolor

提问by Zhen

I have changed my navigation bar color via the following code

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

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

The code worked but the text color also needs to be changed (see screenshot below). Also the refresh button logo on the right is affected as well

代码有效,但文本颜色也需要更改(见下面的截图)。右侧的刷新按钮标志也受到影响

enter image description here

在此处输入图片说明

The same issue occurs if I navigate to another page in the stack

如果我导航到堆栈中的另一个页面,则会出现同样的问题

enter image description here

在此处输入图片说明

Question: How can I change the color of the

问题:我怎样才能改变颜色

  • title text
  • Back button text and
  • right bar button icon color?
  • 标题文字
  • 后退按钮文本和
  • 右栏按钮图标颜色?

After I changed the background color of the navbar?

更改导航栏的背景颜色后?

回答by Erwan

In iOS 7, just use:

在 iOS 7 中,只需使用:

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

Change [UIColor whiteColor] with whatever text color you want

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

回答by Erre Efe

For the title here's the way:

对于标题,方法如下:

iPhone Navigation Bar Title text color

iPhone 导航栏标题文字颜色

And for the custom buttons here's the way:

对于自定义按钮,方法如下:

adding buttons to ui navigation controller bottom bar

向 ui 导航控制器底部栏添加按钮

回答by hasan

To change text color:

要更改文本颜色:

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

Adding refresh button and color it:

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

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

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

Variables that effect navigation bar background:

影响导航栏背景的变量:

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

回答by typeoneerror

I just put together a simple UIViewControllersubclass that adds a customizable back button that allows you to change text colors. It basically adds some willAppear/willDisappearlogic to animate the back button the way the UINavigationControllerdoes while using the leftBarButtonItemproperty. You might extend this to also do the rightBarButtomItem as well.

我只是组合了一个简单的UIViewController子类,它添加了一个可自定义的后退按钮,允许您更改文本颜色。它基本上添加了一些willAppear/willDisappear逻辑,以UINavigationController在使用leftBarButtonItem属性时为后退按钮设置动画。您可以将其扩展为也执行 rightBarButtomItem。

https://github.com/typeoneerror/BBCustomBackButtonViewController

https://github.com/typeoneerror/BBCustomBackButtonViewController