默认情况下,iOS导航栏标题颜色为白色。是否有方法将其更改为其他颜色?
我知道可以使用navigationItem.titleView
方法使用图像的方式进行更改。由于我的设计技能有限并且我未能获得标准的光泽效果,因此我更喜欢更改文本颜色。
非常感谢您的任何见解。
默认情况下,iOS导航栏标题颜色为白色。是否有方法将其更改为其他颜色?
我知道可以使用navigationItem.titleView
方法使用图像的方式进行更改。由于我的设计技能有限并且我未能获得标准的光泽效果,因此我更喜欢更改文本颜色。
非常感谢您的任何见解。
iOS 5及以上版本中,我们需要使用titleTextAttribute字典(UInavigation控制器类参考中的预定义字典)来设置导航栏的标题文本颜色和字体。
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor],UITextAttributeTextColor,
[UIFont fontWithName:@"ArialMT" size:16.0], UITextAttributeFont,nil]];
在任何视图控制器的viewDidLoad或viewWillAppear方法中使用以下代码。
- (void)viewDidLoad
{
[super viewDidLoad];
//I am using UIColor yellowColor for an example but you can use whatever color you like
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};
//change the title here to whatever you like
self.title = @"Home";
// Do any additional setup after loading the view.
}
简短而精炼。
[[[self navigationController] navigationBar] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];
这是基于Stevens的解决方案。
唯一的区别是我加入了一些处理来根据文本长度调整位置,似乎类似于苹果的做法。
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(([self.title length] < 10 ? UITextAlignmentCenter : UITextAlignmentLeft), 0, 480,44)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont boldSystemFontOfSize: 20.0f];
titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleLabel.textAlignment = ([self.title length] < 10 ? UITextAlignmentCenter : UITextAlignmentLeft);
titleLabel.textColor = [UIColor redColor];
titleLabel.text = self.title;
self.navigationItem.titleView = titleLabel;
[titleLabel release];
您可能需要根据字体大小调整10的值。
Swift 4和4.2版本:
self.navigationController.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]
我遇到了导航按钮的问题,当只有一个按钮时,文本会偏离中心。为了解决这个问题,我只需要像下面这样更改框架大小:
CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
我已经自定义了navigationBar的背景图片和左侧按钮,但是灰色标题和背景不匹配。然后我使用如下代码:
[self.navigationBar setTintColor:[UIColor darkGrayColor]];
把色调颜色变成灰色。现在标题是白色的!这就是我想要的。
希望也能帮到你 :)
这是一个相当古老的帖子,但我想提供答案,以设置iOS 7及以上版本导航栏标题的颜色、大小和垂直位置。
关于颜色和大小
NSDictionary *titleAttributes =@{
NSFontAttributeName :[UIFont fontWithName:@"Helvetica-Bold" size:14.0],
NSForegroundColorAttributeName : [UIColor whiteColor]
};
竖直位置
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-10.0 forBarMetrics:UIBarMetricsDefault];
设置标题并分配属性字典
[[self navigationItem] setTitle:@"CLUBHOUSE"];
self.navigationController.navigationBar.titleTextAttributes = titleAttributes;
建议设置self.title,因为在推送子导航栏或在选项卡栏上显示标题时会使用它。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// create and customize title view
self.title = NSLocalizedString(@"My Custom Title", @"");
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.text = self.title;
titleLabel.font = [UIFont boldSystemFontOfSize:16];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel sizeToFit];
self.navigationItem.titleView = titleLabel;
[titleLabel release];
}
}
下面的代码适用于Swift:
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
navigationController?.navigationBar.titleTextAttributes = { 如果当前属性存在,则为当前属性 var newAttributes = currentAttributes newAttributes [NSForegroundColorAttributeName] = navigationTintColor return newAttributes } else {return [NSForegroundColorAttributeName:navigationTintColor]}}()
- Matic Oblakself.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : UIColor.redColor};
- Mike Keskinov