iPhone导航栏标题文字颜色

281 uinavigationbar navigationitem ios

默认情况下,iOS导航栏标题颜色似乎是白色.有没有办法将其改为不同的颜色?

我知道navigationItem.titleView使用图像的方法.由于我的设计技巧有限,而且我没有获得标准光面,我更喜欢更改文字颜色.

任何见解都会非常感激.

Ste*_*her 422

现代的方法

现代方式,对于整个导航控制器......在加载导航控制器的根视图时执行此操作一次.

[self.navigationController.navigationBar setTitleTextAttributes:
   @{NSForegroundColorAttributeName:[UIColor yellowColor]}];
Run Code Online (Sandbox Code Playgroud)

但是,这似乎对后续视图没有影响.

经典的方法

旧的方式,每个视图控制器(这些常量适用于iOS 6,但如果想在iOS 7外观上按照视图控制器进行操作,您将需要相同的方法但具有不同的常量):

您需要使用一个UILabel作为titleViewnavigationItem.

标签应该:

  • 有明确的背景颜色(label.backgroundColor = [UIColor clearColor]).
  • 使用粗体20pt系统字体(label.font = [UIFont boldSystemFontOfSize: 20.0f]).
  • 有50%alpha(label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5])的黑色阴影.
  • 您还需要将文本对齐设置为居中(label.textAlignment = NSTextAlignmentCenter(UITextAlignmentCenter对于较旧的SDK).

将标签文本颜色设置为您想要的任何自定义颜色.您确实需要一种不会导致文本混合成阴影的颜色,这种颜色难以阅读.

我通过反复试验解决了这个问题,但我想出的价值最终太简单了,以至于他们不能成为Apple选择的.:)

如果你想验证这一点,放弃这一代码到initWithNibName:bundle:PageThreeViewController.m苹果的NavBar样品.这将用黄色标签替换文本.除了颜色之外,这应该与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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:另外,阅读Erik B的答案如下.我的代码显示了效果,但是他的代码提供了一种更简单的方法来将其放在现有视图控制器上.

  • 注意:这是设置自定义标题的旧方法,我建议阅读Erik B.的答案 (3认同)
  • 如果使用`sizeWithFont:`将标签框架设置为其文本大小,它将神奇地采用标准标签的自动对齐行为. (2认同)

Ale*_* R. 226

我知道这是一个非常古老的线程,但我认为知道新用户iOS 5带来了一个用于建立标题属性的新属性会很有用.

您可以使用UINavigationBar setTitleTextAttributes来设置字体,颜色,偏移和阴影颜色.

此外,您可以为UINavigationBars整个应用程序中的所有设置相同的默认UINavigationBar标题文本属性.

例如:

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor whiteColor],UITextAttributeTextColor, 
                                            [UIColor blackColor], UITextAttributeTextShadowColor, 
                                            [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
Run Code Online (Sandbox Code Playgroud)

  • 在iOS 7中不推荐使用'UITextAttributeTextColor'.iOS 7密钥为'NSForegroundColorAttributeName' (11认同)

min*_*nux 180

在iOS 5中,您可以通过以下方式更改navigationBar标题颜色:

navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};
Run Code Online (Sandbox Code Playgroud)

  • @BartSimpson我同意!对于iOS 7,将`UITextAttributeTextColor`更新为`NSForegroundColorAttributeName`.奇迹般有效! (8认同)

Eri*_*k B 127

根据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];
}
Run Code Online (Sandbox Code Playgroud)

除了正确处理框架之外,此代码的优点是,如果更改控制器的标题,自定义标题视图也将更新.无需手动更新.

另一个很大的优点是它可以很容易地启用自定义标题颜色.您需要做的就是将此方法添加到控制器.

  • 我同意这个绝对是最好的解决方案.不需要使用sizeWithFont,我喜欢覆盖setTitle方法的想法. (3认同)

gir*_*_vr 40

以上大多数建议现已弃用,iOS 7使用 -

NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
                               [UIColor whiteColor],NSForegroundColorAttributeName, 
                               [UIColor whiteColor],NSBackgroundColorAttributeName,nil];

self.navigationController.navigationBar.titleTextAttributes = textAttributes;
self.title = @"Title of the Page";
Run Code Online (Sandbox Code Playgroud)

另外,检查NSAttributedString.h以获取可以设置的各种文本属性.


Pra*_*esh 38

在IOS 7和8中,您可以更改标题的颜色,让我们说绿色

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor greenColor] forKey:NSForegroundColorAttributeName];
Run Code Online (Sandbox Code Playgroud)

  • Swift: `self.navigationController!.navigationBar.titleTextAttributes = NSDictionary(object: UIColor.whiteColor(), forKey: NSForegroundColorAttributeName) as [NSObject : AnyObject]` (6认同)
  • 在swift 2.0中很容易``self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]` (2认同)

Mic*_*hal 23

为了使问题保持​​最新,我将添加Alex RR解决方案,但在Swift中:

self.navigationController.navigationBar.barTintColor = .blueColor()
self.navigationController.navigationBar.tintColor = .whiteColor()
self.navigationController.navigationBar.titleTextAttributes = [
    NSForegroundColorAttributeName : UIColor.whiteColor()
]
Run Code Online (Sandbox Code Playgroud)

结果是:

在此输入图像描述

  • 在Swift 4.0中:self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white] (2认同)

fuj*_*471 14

方法1,在IB中设置:

在此输入图像描述

方法2,一行代码:

navigationController?.navigationBar.barTintColor = UIColor.blackColor()
Run Code Online (Sandbox Code Playgroud)


Cas*_*ash 13

如果您尝试更改页面上的颜色,tewha的解决方案效果很好,但我希望能够更改每个页面上的颜色.我做了一些小修改,以便它适用于所有页面UINavigationController

NavigationDelegate.h

//This will change the color of the navigation bar
#import <Foundation/Foundation.h>
@interface NavigationDelegate : NSObject<UINavigationControllerDelegate> {
}
@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)


小智 13

从iOS 5开始,我们必须使用titleTextAttribute Dictionary(UInavigation控制器类引用中的预定义字典)设置标题文本颜色和导航栏的字体.

 [[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor blackColor],UITextAttributeTextColor, 
[UIFont fontWithName:@"ArialMT" size:16.0], UITextAttributeFont,nil]];
Run Code Online (Sandbox Code Playgroud)


Mic*_*oos 13

Swift版本

我发现大多数人都提出了Objective_C版本的答案

我想通过使用Swift为任何需要它的人实现这个功能.

在ViewDidload中

1.使NavigationBar背景变为彩色(例如:BLUE)

self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()
Run Code Online (Sandbox Code Playgroud)

2.使NavigationBar背景成为图像(例如:ABC.png)

let barMetrix = UIBarMetrics(rawValue: 0)!

self.navigationController?.navigationBar
      .setBackgroundImage(UIImage(named: "ABC"), forBarMetrics: barMetrix)
Run Code Online (Sandbox Code Playgroud)

3.更改NavigationBar标题(例如:[字体:Futura,10] [颜色:红色])

navigationController?.navigationBar.titleTextAttributes = [
            NSForegroundColorAttributeName : UIColor.redColor(),
            NSFontAttributeName : UIFont(name: "Futura", size: 10)!
        ]
Run Code Online (Sandbox Code Playgroud)

(提示1:不要忘记UIFont之后的"!"标记)

(提示2:标题文本有很多属性,命令点击"NSFontAttributeName"你可以进入类并查看keyNames和他们需要的Objects类型)

我希望我能帮忙!:D


Gau*_*ani 10

在任何视图控制器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.
}
Run Code Online (Sandbox Code Playgroud)


Pie*_*eau 9

简短又甜蜜.

[[[self navigationController] navigationBar] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];
Run Code Online (Sandbox Code Playgroud)


Ant*_*ain 8

这是我基于史蒂文斯的解决方案

唯一真正的不同之处在于,如果根据文字长度的话,我会根据文字长度调整位置,这似乎与苹果的做法类似

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];
Run Code Online (Sandbox Code Playgroud)

您可能需要根据字体大小调整10值


小智 7

斯威夫特 4 和 4.2 版本:

 self.navigationController.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.green]
Run Code Online (Sandbox Code Playgroud)


cor*_*sen 6

我的导航按钮将文本从中心抛出(当你只有一个按钮时)我遇到了问题.为了解决这个问题,我只是改变了我的帧大小:

CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
Run Code Online (Sandbox Code Playgroud)


Shi*_*iny 6

我已经自定义了navigationBar的背景图像和左按钮项,灰色标题不适合背景.然后我用:

[self.navigationBar setTintColor:[UIColor darkGrayColor]];
Run Code Online (Sandbox Code Playgroud)

将色调颜色更改为灰色.现在标题是白色的!这就是我想要的.

希望也能帮助:)


小智 6

建议设置self.title,因为在推送子导航栏或在tabbars上显示标题时使用它.

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)


Abd*_*eed 6

这是一个非常古老的线程,但我想为iOS 7及更高版本提供设置导航栏标题的颜色,大小和垂直位置的答案

颜色和尺寸

 NSDictionary *titleAttributes =@{
                                NSFontAttributeName :[UIFont fontWithName:@"Helvetica-Bold" size:14.0],
                                NSForegroundColorAttributeName : [UIColor whiteColor]
                                };
Run Code Online (Sandbox Code Playgroud)

对于垂直位置

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-10.0 forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

设置标题并分配属性字典

[[self navigationItem] setTitle:@"CLUBHOUSE"];
self.navigationController.navigationBar.titleTextAttributes = titleAttributes;
Run Code Online (Sandbox Code Playgroud)


zum*_*zum 5

这在Swift中对我有用:

navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
Run Code Online (Sandbox Code Playgroud)


Dav*_*raw 2

这是缺失的事情之一。最好的选择是创建自己的自定义导航栏,添加文本框,并以这种方式操纵颜色。