简体   繁体   English

在iOS7中自定义navigationBar - 标题颜色不起作用

[英]Customizing navigationBar in iOS7 - title color not working

I am trying to customize the navigationBar of a navigation controller in iOS7 and the title color is not changing. 我试图在iOS7中自定义导航控制器的navigationBar,标题颜色不会改变。 I am doing the following: 我正在做以下事情:

    [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. navigationBar半透明关闭并且很暗,但标题也保持黑暗。 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? 如何更改标题颜色?

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调用放在你的app delegate的didFinishLaunchingWithOptions方法中。

UPDATE: Might as well post the Swift equivalents. 更新:不妨发布Swift等价物。

To update the navigationBar for an individual view controller, one can use: 要更新单个视图控制器的navigationBar,可以使用:

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()]

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]];
[navigationController.navigationBar setBarStyle:UIBarStyleBlack];

In swift you'll do the following: 在swift中,您将执行以下操作:

self.navigationController.navigationBar.titleTextAttributes = [
    NSForegroundColorAttributeName : UIColor.redColor(),
    NSFontAttributeName : UIFont.systemFontOfSize(20)
]

Swift + UIAppearance version: Swift + UIAppearance版本:

    UINavigationBar.appearance().barTintColor = .blackColor()
    UINavigationBar.appearance().barStyle = .Black

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]}];

actually looks like a bug. 实际上看起来像一个bug。 Try 尝试

myViewcontroller.title = @"";
navigationController.navigationBar.barStyle = UIBarStyleBlack;
myViewcontroller.title = @"TITLE";

works. 作品。

In Swift 4 在Swift 4中

NSForegroundColorAttributeName was renamed to NSAttributedString.Key.foregroundColor NSForegroundColorAttributeName已重命名为NSAttributedString.Key.foregroundColor

So If You're looking to change the navigationItem title's color: 所以,如果您想要更改navigationItem标题的颜色:

let navBarAppearance = self.navigationController!.navigationBar    
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]


I used UIColor.white , just insert any UIColor you want the title to be. 我使用了UIColor.white ,只需插入你想要标题的任何UIColor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM