更改导航栏的背景和文字颜色
//设置NavigationBar背景颜色
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
//@{}代表Dictionary
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
设置导航条Item内容
UIBarButtonItem *item =[[UIBarButtonItem alloc] initWithTitle:@"右边" style:UIBarButtonItemStyleDone target:self action:@selector(click)];
self.navigationItem.rightBarButtonItem = item;
在iOS7之后,系统默认会把导航条上的图片渲染成蓝色.
如何不渲染图片,告诉系统我这个图片不要渲染
UIImage *image = [UIImage imageNamed:@"navigationbar_friendsearch"];
// 返回一个最原始的图片
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 创建一个按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:image forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"navigationbar_friendsearch_highlighted"] forState:UIControlStateHighlighted];
// 导航条上按钮尺寸可以由自己决定
// 导航条上的内容位置由系统决定
btn.frame = CGRectMake(2000, 0, 35, 35);
// 导航条的内容显示两张图片
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = item1;
通过设置navigationItem的backBarButtonItem可以直接更换文字
【注意,要在父视图的Controller中设置】如下:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = item;
设置颜色
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];