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

RyJ*_*RyJ 22 ios

这会导致应用崩溃:

[[UINavigationBar appearance] setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

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

RyJ*_*RyJ 66

这有效:

NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
Run Code Online (Sandbox Code Playgroud)

  • UITextAttributeTextColor和UITextAttributeTextShadowColor不建议使用iOS 7.0+,而是分别使用NSForegroundColorAttributeName和NSShadowAttributeName. (6认同)
  • 效果很好.如果您还需要将主题设置为活动的UINavigationBar,请添加以下行:`[yourViewController.navigationController.navigationBar setTitleTextAttributes:textTitleOptions]` (2认同)