iOS导航栏的标题颜色默认是白色。有没有办法换一种颜色?

我知道navigationItem。使用图像的titleView方法。由于我的设计技能有限,我没有得到标准的光泽,我更喜欢改变文字的颜色。

任何见解都将非常感激。


当前回答

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

唯一真正的区别是,如果根据文本长度,我将一些处理用于调整位置,似乎与苹果的做法类似

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值

其他回答

这是我们缺失的东西之一。您最好的办法是创建自己的自定义导航栏,添加一个文本框,并以这种方式操作颜色。

在任何视图控制器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.
}

在iOS 5中,你可以这样改变navigationBar标题的颜色:

navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};

对于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以获得可以设置的各种文本属性。

我已经自定义了导航栏的背景图像和左键项目,灰色标题不适合背景。然后我使用:

[self.navigationBar setTintColor:[UIColor darkGrayColor]];

将淡色改为灰色。现在标题是白色的!这就是我想要的。

也希望能有所帮助:)