3 回答
TA贡献1820条经验 获得超3个赞
现代的方法
现代方式,对于整个导航控制器......在加载导航控制器的根视图时执行此操作一次。
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor yellowColor]}];但是,这似乎对后续视图没有影响。
经典的方法
旧的方式,每个视图控制器(这些常量适用于iOS 6,但如果想在iOS 7外观上按照视图控制器进行操作,您将需要相同的方法但具有不同的常量):
您需要使用一个UILabel作为titleView的navigationItem。
标签应该:
有明确的背景颜色(
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)。
将标签文本颜色设置为您想要的任何自定义颜色。您确实需要一种不会导致文本混合成阴影的颜色,这种颜色难以阅读。
我通过反复试验解决了这个问题,但我想出的价值最终太简单了,以至于他们不能成为苹果选择的。:)
如果你想验证这一点,放弃这一代码到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;}编辑:另外,阅读Erik B的答案如下。我的代码显示了效果,但是他的代码提供了一种更简单的方法来将其放在现有视图控制器上。
TA贡献1906条经验 获得超10个赞
我知道这是一个非常古老的线程,但我认为知道新用户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];
TA贡献1821条经验 获得超5个赞
在iOS 5中,您可以通过以下方式更改navigationBar标题颜色:
navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};- 3 回答
- 0 关注
- 1072 浏览
添加回答
举报
