默认情况下,iOS导航栏标题颜色为白色。是否有方法将其更改为其他颜色?
我知道可以使用navigationItem.titleView
方法使用图像的方式进行更改。由于我的设计技能有限并且我未能获得标准的光泽效果,因此我更喜欢更改文本颜色。
非常感谢您的任何见解。
默认情况下,iOS导航栏标题颜色为白色。是否有方法将其更改为其他颜色?
我知道可以使用navigationItem.titleView
方法使用图像的方式进行更改。由于我的设计技能有限并且我未能获得标准的光泽效果,因此我更喜欢更改文本颜色。
非常感谢您的任何见解。
现代的方式,对于整个导航控制器...在您的导航控制器根视图加载时执行此操作。
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor yellowColor]}];
然而,这似乎对后续视图没有影响。
旧的方式是针对每个视图控制器(这些常量是用于iOS 6的,但如果要在iOS 7上以每个视图控制器为基础进行设置,则需要采取相同的方法但使用不同的常量):
您需要使用一个UILabel
作为navigationItem
的titleView
。
标签应该:
label.backgroundColor = [UIColor clearColor]
)。label.font = [UIFont boldSystemFontOfSize: 20.0f]
)。label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]
)。label.textAlignment = NSTextAlignmentCenter
(对于旧的SDK,请使用UITextAlignmentCenter
)。将标签文字颜色设置为任何自定义颜色。您需要选择一种颜色,使文本不会与阴影混合在一起,这会使其难以阅读。
我通过尝试和错误来解决这个问题,但我得出的值最终过于简单,以至于它们不是苹果选定的值。 :)
如果您想要验证此内容,请将此代码放入Apple的导航栏示例中的PageThreeViewController.m
中的initWithNibName:bundle:
中。这将用黄色标签替换文本。除了颜色之外,这应该与Apple代码生成的原始标签无法区分。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// this will appear as the title in the navigation bar
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = NSTextAlignmentCenter;
// ^-Use UITextAlignmentCenter for older SDKs.
label.textColor = [UIColor yellowColor]; // change this color
self.navigationItem.titleView = label;
label.text = NSLocalizedString(@"PageThreeTitle", @"");
[label sizeToFit];
}
return self;
}
编辑:此外,请阅读下面Erik B的答案。我的代码展示了效果,但他的代码提供了一种更简单的方法将其放置在现有视图控制器中。
sizeWithFont:
将标签框架设置为其文本大小,则它将神奇地采用标准标签的自动对齐行为。 - grahamparks我知道这是一个很老的帖子,但我认为对于新用户来说,了解iOS 5引入了一种用于建立标题属性的新属性会很有用。
您可以使用setTitleTextAttributes
设置字体、颜色、偏移和阴影颜色来设置UINavigationBar的标题属性。
此外,您还可以为整个应用程序中的所有UINavigationBars
设置相同的默认UINavigationBar标题文本属性。
例如:
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};
UITextAttributeTextColor
更新为NSForegroundColorAttributeName
。非常好用! - John Erckself.navigationController.navigationBar.tintColor
对我没有起作用。这个可以。 - JRam13参考Steven Fisher的答案,我写了这段代码:
- (void)setTitle:(NSString *)title
{
[super setTitle:title];
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
titleView = [[UILabel alloc] initWithFrame:CGRectZero];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleView.textColor = [UIColor yellowColor]; // Change to desired color
self.navigationItem.titleView = titleView;
[titleView release];
}
titleView.text = title;
[titleView sizeToFit];
}
这段代码的优点,除了可以正确处理框架外,还包括:如果您更改控制器的标题,则自定义标题视图也将被更���。无需手动更新。
另一个重要的优势是它使自定义标题颜色变得非常简单。您只需要将此方法添加到控制器即可。
以上大部分建议现在已被废弃,在iOS 7中使用 -
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName,
[UIColor whiteColor],NSBackgroundColorAttributeName,nil];
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
self.title = @"Title of the Page";
还有,请查看NSAttributedString.h以获取可以设置的各种文本属性。
在 iOS 7 和 8 中,您可以将标题的颜色更改为绿色。
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor greenColor] forKey:NSForegroundColorAttributeName];
self.navigationController!.navigationBar.titleTextAttributes = NSDictionary(object: UIColor.whiteColor(), forKey: NSForegroundColorAttributeName) as [NSObject : AnyObject]
self.navigationController!.navigationBar.titleTextAttributes = NSDictionary(object: UIColor.whiteColor(), forKey: NSForegroundColorAttributeName) as [NSObject : AnyObject]
- Byron Coetseeself.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
。 - Kevin DiTragliaself.navigationController.navigationBar.barTintColor = .blueColor()
self.navigationController.navigationBar.tintColor = .whiteColor()
self.navigationController.navigationBar.titleTextAttributes = [
NSForegroundColorAttributeName : UIColor.whiteColor()
]
这导致的结果是:
Swift版本
我发现大部分人提供的答案都是Objective-C版本的。
我想为那些需要的人使用Swift实现这个函数。
在ViewDidload中:
1.将NavigationBar的背景颜色更改为蓝色(例如:BLUE)。
self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()
2.将NavigationBar的背景变为图片(例如:ABC.png)
let barMetrix = UIBarMetrics(rawValue: 0)!
self.navigationController?.navigationBar
.setBackgroundImage(UIImage(named: "ABC"), forBarMetrics: barMetrix)
3. 更改NavigationBar标题(例如:[字体:Futura,10] [颜色:红色])
navigationController?.navigationBar.titleTextAttributes = [
NSForegroundColorAttributeName : UIColor.redColor(),
NSFontAttributeName : UIFont(name: "Futura", size: 10)!
]
(提示1:别忘了在UIFont后面加上"!"号)
(提示2:标题文本有很多属性,请单击“NSFontAttributeName”以进入类并查看keyNames和它们所需的对象类型)
我希望我能帮忙!:D
tewha的解决方案可以很好地在页面上更改颜色,但我想能够在每个页面上更改颜色。我进行了一些小修改,使其适用于UINavigationController
上的所有页面。
NavigationDelegate.h
//This will change the color of the navigation bar
#import <Foundation/Foundation.h>
@interface NavigationDelegate : NSObject<UINavigationControllerDelegate> {
}
@end
导航代理 NavigationDelegate.m
#import "NavigationDelegate.h"
@implementation NavigationDelegate
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
CGRect frame = CGRectMake(0, 0, 200, 44);//TODO: Can we get the size of the text?
UILabel* label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor yellowColor];
//The two lines below are the only ones that have changed
label.text=viewController.title;
viewController.navigationItem.titleView = label;
}
@end