更改導(dǎo)航欄的背景和文字顏色
//設(shè)置NavigationBar背景顏色
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
//@{}代表Dictionary
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
設(shè)置導(dǎo)航條Item內(nèi)容
UIBarButtonItem *item =[[UIBarButtonItem alloc] initWithTitle:@"右邊" style:UIBarButtonItemStyleDone target:self action:@selector(click)];
self.navigationItem.rightBarButtonItem = item;
在iOS7之后,系統(tǒng)默認(rèn)會(huì)把導(dǎo)航條上的圖片渲染成藍(lán)色.
如何不渲染圖片,告訴系統(tǒng)我這個(gè)圖片不要渲染
UIImage *image = [UIImage imageNamed:@"navigationbar_friendsearch"];
// 返回一個(gè)最原始的圖片
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 創(chuàng)建一個(gè)按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:image forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"navigationbar_friendsearch_highlighted"] forState:UIControlStateHighlighted];
// 導(dǎo)航條上按鈕尺寸可以由自己決定
// 導(dǎo)航條上的內(nèi)容位置由系統(tǒng)決定
btn.frame = CGRectMake(2000, 0, 35, 35);
// 導(dǎo)航條的內(nèi)容顯示兩張圖片
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = item1;
通過設(shè)置navigationItem的backBarButtonItem可以直接更換文字
【注意,要在父視圖的Controller中設(shè)置】如下:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = item;
設(shè)置顏色
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];