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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11004700/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:45:21  来源:igfitidea点击:

iOS - Globally change navigation bar title color using appearance?

ios

提问by RyJ

This crashes the app:

这会使应用程序崩溃:

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

Is there a way to do this using appearance?

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

回答by RyJ

This worked:

这有效:

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

回答by Iya

Here's an example of how to do this in Swift:

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

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

回答by Ryan Poolos

That crashes the app before UINavigationBar doesn't have a title or state... Those are UIButton methods

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

You need

你需要

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

回答by Max MacLeod

The @RyJ answer is great and worked for me. Thought I'd chip in that there's a good tutorial on this in Ray Wenderlich's site, titled (excuse the pun):

@RyJ 的回答很棒,对我有用。我想我会说在 Ray Wenderlich 的网站上有一个很好的教程,标题为(请原谅双关语):

User Interface Customization in iOS 6

iOS 6 中的用户界面自定义

See the section Customizing UINavigationBar

请参阅自定义 UINavigationBar部分

Here's the code snippet for the navigation bar title, to change globally:

这是导航栏标题的代码片段,全局更改:

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

One other minor point is that it seems there's a default shadow on the title bar, so to get rid of it, you can't just remove the attribute. Instead you have to set a shadow offset:

另一个小问题是标题栏上似乎有一个默认阴影,因此要摆脱它,您不能只删除该属性。相反,您必须设置阴影偏移:

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

回答by Muhammad Nabeel Arif

I used following code to change the title bar's color.

我使用以下代码来更改标题栏的颜色。

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

回答by T Blank

Using modern syntax and code that actually runs, this is how to globally style your UINavigationBartitle text:

使用实际运行的现代语法和代码,这是如何全局设置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 }];

Note: NSShadow's shadowBlurRadiusproperty is not respected.

注意:NSShadowshadowBlurRadius属性不受尊重。

Note: Shadows are so iOS 6. Don't ever use them.

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