Ios 导航栏标题为黑色
我正在我的应用程序中使用带有Ios 导航栏标题为黑色,ios,iphone,cocoa-touch,uinavigationcontroller,Ios,Iphone,Cocoa Touch,Uinavigationcontroller,我正在我的应用程序中使用带有uitabar的UINavigationBar。 在第一个选项卡上,导航栏标题正确地显示为带有默认背景色的白色标题,但在第二个选项卡上,它的工作方式不同。我没有使用任何代码来更改标题颜色或导航栏的tintColor 第一视图: 第二种观点: 为什么第二个视图的UINavigationBar标题用这种黑色绘制?通常,您不能更改UINavigationBar标题的默认颜色。如果要更改UINavigationBar标题的颜色,则需要自定义UINavigationBar。
uitabar
的UINavigationBar
。
在第一个选项卡上,导航栏标题正确地显示为带有默认背景色的白色标题,但在第二个选项卡上,它的工作方式不同。我没有使用任何代码来更改标题颜色或导航栏的tintColor
第一视图:第二种观点:
为什么第二个视图的
UINavigationBar
标题用这种黑色绘制?通常,您不能更改UINavigationBar
标题的默认颜色。如果要更改UINavigationBar标题的颜色,则需要自定义UINavigationBar。因此,请为第二个ViewController编写代码,以便更好地理解
编辑:
搜索后,我发现您可以通过以下方式更改UINavigationBar
的标题颜色
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
此代码在iOS5及更高版本中工作。此代码更改所有导航栏的文本,使用此代码可以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]];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
这将允许您更改颜色
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
对于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中可以设置的各种文本属性 将此代码位用于iOS7,因为
uitexttributetextcolor
已被弃用
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:NSForegroundColorAttributeName];
在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]];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
我想你应该在第二个视图中设置导航栏标题的颜色。检查我试图更改标题的颜色,但它不起作用。第二个视图控制器代码不会影响另一件事,如果我使用新的viewController而没有任何代码,同样的问题也会发生…@rivan如果你在iOS 5上工作,那么将此代码放入ViewDidLoad方法中我在我的项目中试过它,它对我有效:)这不是关于文本为什么已经改变颜色的问题,不是如何定制?@JamesP Yaa,这不是关于如何更改导航栏标题颜色。对于iOS7,您必须将UITextAttributeTextColor更改为NSForeGroundColorAttributeName。当我使用您的代码时,它将firstview navigationController标题更改为黑色,并更改firstNavigation下的所有视图(在推送视图之后)都是白色的。这对我很有用。我正要发布这个更新的iO7补丁,放弃了投票!谢谢这是正确的答案。你想在外观上设置它,就像在一个普通的应用程序中一样,你真的希望所有的导航栏看起来都一样。