iOS导航栏的标题颜色默认是白色。有没有办法换一种颜色?
我知道navigationItem。使用图像的titleView方法。由于我的设计技能有限,我没有得到标准的光泽,我更喜欢改变文字的颜色。
任何见解都将非常感激。
iOS导航栏的标题颜色默认是白色。有没有办法换一种颜色?
我知道navigationItem。使用图像的titleView方法。由于我的设计技能有限,我没有得到标准的光泽,我更喜欢改变文字的颜色。
任何见解都将非常感激。
当前回答
像这样使用定向支持
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,40)];
[view setBackgroundColor:[UIColor clearColor]];
[view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];
UILabel *nameLabel = [[UILabel alloc] init];
[nameLabel setFrame:CGRectMake(0, 0, 320, 40)];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[nameLabel setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin];
[nameLabel setTextColor:[UIColor whiteColor]];
[nameLabel setFont:[UIFont boldSystemFontOfSize:17]];
[nameLabel setText:titleString];
[nameLabel setTextAlignment:UITextAlignmentCenter];
[view addSubview:nameLabel];
[nameLabel release];
self.navigationItem.titleView = view;
[view release];
其他回答
我相信设置UINavigationBar颜色的正确方法是:
NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],UITextAttributeTextColor, nil];
self.titleTextAttributes = attributes;
上面写的代码是UINavigationBar上的子类,显然没有子类也可以工作。
我遇到了一个问题,我的导航按钮将文本抛出中心(当你只有一个按钮时)。为了解决这个问题,我改变了我的帧大小,如下所示:
CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
titleTextAttributes 显示栏标题文本的属性。
NSDictionary *titleTextAttributes (nonatomic, copy 讨论 你可以在文本属性字典中为标题指定字体、文本颜色、文本阴影颜色和文本阴影偏移量,使用NSString UIKit添加参考中描述的文本属性键。
可用性 支持iOS 5.0及更高版本。 中声明 UINavigationBar.h
这适用于我在Swift:
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
从ios5开始,我们必须使用titleTextAttribute字典(UInavigation控制器类引用中的预定义字典)设置标题文本的颜色和导航栏的字体。
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor],UITextAttributeTextColor,
[UIFont fontWithName:@"ArialMT" size:16.0], UITextAttributeFont,nil]];