iOS导航栏的标题颜色默认是白色。有没有办法换一种颜色?
我知道navigationItem。使用图像的titleView方法。由于我的设计技能有限,我没有得到标准的光泽,我更喜欢改变文字的颜色。
任何见解都将非常感激。
iOS导航栏的标题颜色默认是白色。有没有办法换一种颜色?
我知道navigationItem。使用图像的titleView方法。由于我的设计技能有限,我没有得到标准的光泽,我更喜欢改变文字的颜色。
任何见解都将非常感激。
当前回答
在IOS 7和8中,你可以将标题的颜色改为绿色
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor greenColor] forKey:NSForegroundColorAttributeName];
其他回答
建议设置self。标题,因为这是用在推子导航栏或显示标签栏标题。
- (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];
}
}
我知道这是一个相当老的话题,但我认为这对新用户来说是有用的,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];
可以在appdelegate文件中使用此方法,并可以在每个视图中使用
+(UILabel *) navigationTitleLable:(NSString *)title
{
CGRect frame = CGRectMake(0, 0, 165, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = NAVIGATION_TITLE_LABLE_SIZE;
label.shadowColor = [UIColor whiteColor];
label.numberOfLines = 2;
label.lineBreakMode = UILineBreakModeTailTruncation;
label.textAlignment = UITextAlignmentCenter;
[label setShadowOffset:CGSizeMake(0,1)];
label.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
//label.text = NSLocalizedString(title, @"");
return label;
}
这适用于我在Swift:
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
亚历克斯·R·R的最新消息。的帖子使用新的iOS 7文本属性和现代objective c减少噪音:
NSShadow *titleShadow = [[NSShadow alloc] init];
titleShadow.shadowColor = [UIColor blackColor];
titleShadow.shadowOffset = CGSizeMake(-1, 0);
NSDictionary *navbarTitleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],
NSShadowAttributeName:titleShadow};
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];