iOS导航栏的标题颜色默认是白色。有没有办法换一种颜色?
我知道navigationItem。使用图像的titleView方法。由于我的设计技能有限,我没有得到标准的光泽,我更喜欢改变文字的颜色。
任何见解都将非常感激。
iOS导航栏的标题颜色默认是白色。有没有办法换一种颜色?
我知道navigationItem。使用图像的titleView方法。由于我的设计技能有限,我没有得到标准的光泽,我更喜欢改变文字的颜色。
任何见解都将非常感激。
当前回答
如果你试图改变一个页面的颜色,tewha的解决方案效果很好,但我希望能够改变每个页面的颜色。我做了一些小的修改,使它可以工作在UINavigationController上的所有页面
NavigationDelegate.h
//This will change the color of the navigation bar
#import <Foundation/Foundation.h>
@interface NavigationDelegate : NSObject<UINavigationControllerDelegate> {
}
@end
NavigationDelegate.m
#import "NavigationDelegate.h"
@implementation NavigationDelegate
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
CGRect frame = CGRectMake(0, 0, 200, 44);//TODO: Can we get the size of the text?
UILabel* label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor yellowColor];
//The two lines below are the only ones that have changed
label.text=viewController.title;
viewController.navigationItem.titleView = label;
}
@end
其他回答
在IOS 7和8中,你可以将标题的颜色改为绿色
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor greenColor] forKey:NSForegroundColorAttributeName];
方法一,设置为IB:
方法二,一行代码:
navigationController?.navigationBar.barTintColor = UIColor.blackColor()
在任何视图控制器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.
}
我相信设置UINavigationBar颜色的正确方法是:
NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],UITextAttributeTextColor, nil];
self.titleTextAttributes = attributes;
上面写的代码是UINavigationBar上的子类,显然没有子类也可以工作。
这适用于我在Swift:
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]