objective-c 在 iOS7 中自定义导航栏 - 标题颜色不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19392803/
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
Customizing navigationBar in iOS7 - title color not working
提问by Live2Enjoy7
I am trying to customize the navigationBar of a navigation controller in iOS7 and the title color is not changing. I am doing the following:
我正在尝试在 iOS7 中自定义导航控制器的 navigationBar 并且标题颜色没有改变。我正在做以下事情:
[navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:46.0f/256.0f green:46.0f/256.0f blue:46.0f/256.0f alpha:1]];
[navigationController.navigationBar setTranslucent:NO];
[navigationController.navigationBar setTitleTextAttributes:@{[UIColor whiteColor]:UITextAttributeTextColor}];
[self presentViewController:navigationController animated:YES completion:nil];
The navigationBar translucency gets turned off and it is dark, but the title also stays dark. I've also tried to create a custom label and set it as the title view with not much luck.
导航栏半透明被关闭,它是黑暗的,但标题也保持黑暗。我还尝试创建一个自定义标签并将其设置为标题视图,但运气不佳。
How can the title color be changed?
如何更改标题颜色?
采纳答案by Live2Enjoy7
[navigationController.navigationBar setBarStyle:UIBarStyleBlack];
回答by Mark Semsel
The appearance api continues to evolve, and UITextAttributeTextColor is now replaced with NSForegroundColorAttributeName.
外观 api 不断发展,现在 UITextAttributeTextColor 替换为 NSForegroundColorAttributeName。
[navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
I'll add two things:
The key comes first, then the object.
我将添加两件事:
首先是键,然后是对象。
If you want to globally change your nav controller's title attributes, use the appearance api:
如果要全局更改导航控制器的标题属性,请使用外观 api:
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
I'd put that appearance api call in your app delegate's didFinishLaunchingWithOptions method.
我会将外观 api 调用放在您的应用程序委托的 didFinishLaunchingWithOptions 方法中。
UPDATE: Might as well post the Swift equivalents.
更新:不妨发布 Swift 等价物。
To update the navigationBar for an individual view controller, one can use:
要更新单个视图控制器的导航栏,可以使用:
self.navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
To change the navigation bar's appearance throughout an entire app, one can use:
要在整个应用程序中更改导航栏的外观,可以使用:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
回答by wolffan
Not working for me. Using UINavigation as a modal view controller.
不对我来说有效。使用 UINavigation 作为模态视图控制器。
Apparently title colour needs to be set uiapplication level
显然标题颜色需要设置 uiapplication 级别
Edit:
编辑:
Best way is to change the navigation title in each VC context:
最好的方法是在每个 VC 上下文中更改导航标题:
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]];
回答by Antoine
In swift you'll do the following:
在 swift 中,您将执行以下操作:
self.navigationController.navigationBar.titleTextAttributes = [
NSForegroundColorAttributeName : UIColor.redColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)
]
回答by superarts.org
Swift + UIAppearance version:
Swift + UIAppearance 版本:
UINavigationBar.appearance().barTintColor = .blackColor()
UINavigationBar.appearance().barStyle = .Black
回答by mhoeller
The problem in the OP's code is this line:
OP 代码中的问题是这一行:
[navigationController.navigationBar setTitleTextAttributes:@{[UIColor whiteColor]:UITextAttributeTextColor}];
The text color attribute name and value are mixed up. It should be
文本颜色属性名称和值混淆。它应该是
[navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeTextColor:[UIColor whiteColor]}];
回答by Richard Poutier
In Swift 4
在斯威夫特 4
NSForegroundColorAttributeNamewas renamed to NSAttributedString.Key.foregroundColor
NSForegroundColorAttributeName被重命名为NSAttributedString.Key.foregroundColor
So If You're looking to change the navigationItem title's color:
因此,如果您要更改导航项标题的颜色:
let navBarAppearance = self.navigationController!.navigationBar
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
I used UIColor.white, just insert any UIColoryou want the title to be.
我使用UIColor.white,只需插入您想要标题的任何UIColor。
回答by Jason
actually looks like a bug. Try
实际上看起来像一个错误。尝试
myViewcontroller.title = @"";
navigationController.navigationBar.barStyle = UIBarStyleBlack;
myViewcontroller.title = @"TITLE";
works.
作品。

