9

UINavigationBar在我的应用程序中使用UITabBar. 在第一个选项卡上,导航栏标题正确显示为具有默认背景颜色的白色标题,但在第二个选项卡上,它的工作方式不同。我没有使用任何代码来更改标题颜色或导航栏的tintColor.

第一个视图:http:
//img407.imageshack.us/img407/7192/4go.png

第二种观点:

为什么第二个视图的UINavigationBar标题用这种黑色绘制?

4

6 回答 6

26

通常,您不能更改UINavigationBar标题的默认颜色。如果您想更改 UINavigationBar Title 的颜色,则需要自定义 UINavigationBar。因此,为您的第二个 ViewController 放置代码以获得更多理解。

编辑:

经过搜索,我发现 You can change title color of UINavigationBarby

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

此代码适用于iOS5 及更高版本。

于 2013-06-28T07:15:52.237 回答
10

大多数上述建议现在已弃用,适用于 iOS 7 -

NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
                               [UIColor whiteColor],NSForegroundColorAttributeName, 
                               [UIColor whiteColor],NSBackgroundColorAttributeName,nil];

self.navigationController.navigationBar.titleTextAttributes = textAttributes;
self.title = @"Title of the Page";

此外,检查 NSAttributedString.h 以了解可以设置的各种文本属性。

于 2013-10-03T12:12:48.463 回答
8

在 AppDelegate 中试试这个:

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
于 2014-03-06T02:21:55.237 回答
7

这将允许您更改颜色

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIColor whiteColor],UITextAttributeTextColor, 
                                        [UIColor blackColor],      UITextAttributeTextShadowColor, 
                                        [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
于 2013-06-30T05:26:08.083 回答
4

将这段代码用于 iOS7UITextAttributeTextColor已被弃用。

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:NSForegroundColorAttributeName];
于 2014-01-07T05:28:07.093 回答
1

此代码更改了所有导航栏的文本,使用此代码可以 100% 自定义文本。

在 appDelegate 中:

 //custom text navBar
   [[UINavigationBar appearance] setTitleTextAttributes: 
   [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0x73/255.0 green:0x47/255.0 blue:0x41/255.0 alpha:1.0], UITextAttributeTextColor,
   [UIColor colorWithRed:0x1D/255.0 green:0x1D/255.0 blue:0x1B/255.0 alpha:1], UITextAttributeTextShadowColor,
   [NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset, 
   [UIFont fontWithName:@"yourFont" size:20], UITextAttributeFont, nil]];
于 2013-06-28T15:14:17.217 回答