[英]NavigationBar title is coming in black color
我在我的应用程序中使用UINavigationBar
和UITabBar
。 在第一个选项卡上,导航栏标题正确地作为带有默认背景颜色的白色标题,但在第二个选项卡上它不能以相同的方式工作。 我没有使用任何代码来更改标题颜色或导航栏的tintColor
。
第一种观点:
http://img407.imageshack.us/img407/7192/4go.png
为什么第二个视图的UINavigationBar
标题是用这种黑色绘制的?
通常,您无法更改UINavigationBar
Title的默认颜色。 如果你想改变UINavigationBar Title的颜色,你需要自定义UINavigationBar。 所以为你的第二个ViewController添加代码以获得更多理解。
编辑:
搜索后,我发现你可以改变UINavigationBar
的标题颜色
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
此代码适用于iOS5及更高版本。
以上大多数建议现已弃用,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以获取可以设置的各种文本属性。
在AppDelegate中试试这个:
[[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];
使用iOS7的这段代码,因为不推荐使用UITextAttributeTextColor
。
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:NSForegroundColorAttributeName];
此代码更改了所有navigationBar的文本,使用此代码可以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]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.