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

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

任何见解都将非常感激。


当前回答

你应该调用[label sizeToFit];设置文本后,防止奇怪偏移时,当其他按钮占据导航栏时,标签自动在标题视图中重新定位。

其他回答

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

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

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

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

在任何视图控制器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 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以获得可以设置的各种文本属性。

你应该调用[label sizeToFit];设置文本后,防止奇怪偏移时,当其他按钮占据导航栏时,标签自动在标题视图中重新定位。