[英]iOS - Globally change navigation bar title color using appearance?
This crashes the app:这会使应用程序崩溃:
[[UINavigationBar appearance] setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
Is there a way to do this using appearance?有没有办法使用外观来做到这一点?
This worked: 这有效:
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
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()]
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]];
The @RyJ answer is great and worked for me. @RyJ答案很棒,对我有用。 Thought I'd chip in that there's a good tutorial on this in Ray Wenderlich's site, titled (excuse the pun):
我想在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)]
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];
Using modern syntax and code that actually runs, this is how to globally style your UINavigationBar
title 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 shadowBlurRadius
property is not respected. 注意:
NSShadow
的shadowBlurRadius
属性不受尊重。
Note: Shadows are so iOS 6. Don't ever use them. 注意:阴影是如此iOS 6.不要使用它们。
for iOS 15适用于 iOS 15
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = <PREFERRED BACKGROUND COLOR>
appearance.titleTextAttributes = [.foregroundColor : <PREFERRED TITLE COLOR>]
navigationBar.tintColor = <PREFERED TINT COLOR> //for bar buttons
navigationBar.standardAppearance = appearance;
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.