iOS – 使用外观全局更改导航栏标题颜色?

这使应用程序崩溃:

[[UINavigationBar appearance] setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal]; 

有没有办法做到这一点使用外观?

这工作:

 NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil]; [[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions]; 

下面是一个如何在Swift中执行此操作的示例:

 UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName:UIFont(name:"Exo2-Bold", size: 18) as! AnyObject, NSForegroundColorAttributeName:UIColor.whiteColor()] 

这在UINavigationBar没有标题或状态之前崩溃的应用程序…这些都是UIButton方法

你需要

 [[UINavigationBar appearance] setTintColor:[UIColor darkGrayColor]]; 

@RyJ的答案是伟大的,为我工作。 我以为我会认为雷文德里希的网站有一个很好的教程,标题是(原谅双关语):

iOS 6中的用户界面自定义

请参阅自定义UINavigationBar一节

以下是导航栏标题的代码段,以便全局更改:

 // Customize the title text for *all* UINavigationBars [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset, [UIFont fontWithName:@"Arial-Bold" size:0.0], UITextAttributeFont, nil]]; 

另一个小问题是,标题栏上似乎有一个默认的阴影,所以为了摆脱它,你不能只删除该属性。 相反,你必须设置一个阴影偏移量:

 UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(0, 0)] 

我用下面的代码来改变标题栏的颜色。

 NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowColor = [UIColor blackColor]; shadow.shadowOffset = CGSizeMake(1, 0); NSDictionary *titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor], NSShadowAttributeName:shadow}; [[UINavigationBar appearance] setTitleTextAttributes:titleTextAttributes]; 

使用实际运行的现代语法和代码,这是如何全局样式您的UINavigationBar标题文本:

 NSShadow *navigationBarTitleShadow = [[NSShadow alloc] init]; navigationBarTitleShadow.shadowColor = [UIColor colorWithWhite:0.5 alpha:0.5]; navigationBarTitleShadow.shadowOffset = CGSizeMake(2.0, 2.0); [[UINavigationBar appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont fontWithName:@"Arial-BoldMT" size:30.0], NSShadowAttributeName : navigationBarTitleShadow }]; 

注意: NSShadowshadowBlurRadius属性不受尊重。

注意:阴影是如此的iOS 6.永远不要使用它们。