iOS导航栏的标题颜色默认是白色。有没有办法换一种颜色?
我知道navigationItem。使用图像的titleView方法。由于我的设计技能有限,我没有得到标准的光泽,我更喜欢改变文字的颜色。
任何见解都将非常感激。
iOS导航栏的标题颜色默认是白色。有没有办法换一种颜色?
我知道navigationItem。使用图像的titleView方法。由于我的设计技能有限,我没有得到标准的光泽,我更喜欢改变文字的颜色。
任何见解都将非常感激。
当前回答
我相信设置UINavigationBar颜色的正确方法是:
NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],UITextAttributeTextColor, nil];
self.titleTextAttributes = attributes;
上面写的代码是UINavigationBar上的子类,显然没有子类也可以工作。
其他回答
这是我们缺失的东西之一。您最好的办法是创建自己的自定义导航栏,添加一个文本框,并以这种方式操作颜色。
要设置标题的字体大小,我使用了以下条件..也许对任何人都有帮助
if ([currentTitle length]>24) msize = 10.0f;
else if ([currentTitle length]>16) msize = 14.0f;
else if ([currentTitle length]>12) msize = 18.0f;
我遇到了一个问题,我的导航按钮将文本抛出中心(当你只有一个按钮时)。为了解决这个问题,我改变了我的帧大小,如下所示:
CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
根据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];
}
这段代码的优点,除了正确地处理帧,是如果你改变你的控制器的标题,自定义标题视图也会得到更新。无需手动更新。
另一个很大的优势是,它使得启用自定义标题颜色非常简单。您所需要做的就是将这个方法添加到控制器中。
方法一,设置为IB:
方法二,一行代码:
navigationController?.navigationBar.barTintColor = UIColor.blackColor()