在iOS7下缘琅,默認(rèn)導(dǎo)航欄背景,顏色是這樣的廓推,接下來我們就進(jìn)行自定義刷袍,如果你僅僅是更改一下背景和顏色,代碼會(huì)很簡單樊展,不需要很復(fù)雜的自定義View來替代leftBarItem
更改導(dǎo)航欄的背景和文字Color
方法一:
[objc] view plaincopy
//set NavigationBar 背景顏色&title 顏色
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0]];
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
效果如下:
我們把背景改成了藍(lán)色呻纹,title文字改成了白色,是不是很簡單呢专缠?NavigationBar極其push過去的子頁面也會(huì)是你修改后的背景顏色
方法二:
[objc] view plaincopy
//設(shè)置NavigationBar背景顏色
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
//@{}代表Dictionary
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
在導(dǎo)航欄使用背景圖片:
如果您的應(yīng)用程序使用了自定義圖像作為欄的背景雷酪,你需要提供一個(gè)“更大”的圖片,使其延伸了狀態(tài)欄的后面涝婉。導(dǎo)航欄的高度現(xiàn)在是從44點(diǎn)(88像素)更改為64點(diǎn)(128像素)太闺。
仍然可以使用了setBackgroundImage:方法來指定自定義圖像的導(dǎo)航欄。下面是代碼行設(shè)置背景圖片:
[objc] view plaincopy
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];
效果圖和上面的一樣嘁圈,我就不貼出來了省骂。
改變導(dǎo)航欄標(biāo)題的字體
就像iOS 6,我們可以通過使用導(dǎo)航欄的“titleTextAttributes”屬性來自定義的文本樣式最住〕模可以指定字體,文字顏色涨缚,文字陰影顏色轧粟,文字陰影在文本標(biāo)題偏移屬性字典,使用下面的文本屬性鍵:
UITextAttributeFont - 字體
UITextAttributeTextColor - 文字顏色
UITextAttributeTextShadowColor - 文字陰影顏色
UITextAttributeTextShadowOffset - 偏移用于文本陰影
[objc] view plaincopy
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,
[UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil nil]];
使用圖片作為導(dǎo)航欄標(biāo)題
不想標(biāo)題欄是光禿禿的文字脓魏?可以通過使用代碼行中的圖像或標(biāo)志取代它:簡單地改變titleview用來自定義兰吟,(適用于較低版本)
[objc] view plaincopy
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"appcoda-logo.png"]];
添加多個(gè)欄按鈕項(xiàng)目
您希望添加導(dǎo)航欄的一側(cè)不止一個(gè)欄按鈕項(xiàng)目,無論是leftBarButtonItems和rightBarButtonItems 您在導(dǎo)航欄左側(cè)/右側(cè)指定自定義欄按鈕項(xiàng)目茂翔。比如你想添加一個(gè)攝像頭和一個(gè)共享按鈕右側(cè)的吧混蔼。您可以使用下面的代碼:
[objc] view plaincopy
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action: nil nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action: nil nil];
NSArray *itemsArr = @[shareItem,cameraItem];
self.navigationItem.rightBarButtonItems = itemsArr;
自定義后退按鈕的文字和顏色
通常情況下,我們使用UINavigationController時(shí)珊燎,push到的子頁面惭嚣,左上角會(huì)是系統(tǒng)自動(dòng)取值上一層父頁面的title名稱,默認(rèn)情況是這樣悔政,那么我們?cè)撊绾涡薷乃兀?/p>
左側(cè)顯示了父頁面的title:用戶登錄晚吞,可是我們想修改成返回,方式有很多谋国,舉些例子
方法一:
通過設(shè)置navigationItem的backBarButtonItem可以直接更換文字槽地,【注意峭咒,要在父視圖的Controller中設(shè)置】如下:
[objc] view plaincopy
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = item;
效果如下:
所有的子界面返回時(shí)都變成了我們定義的文字怎静,如果不想顯示文字富纸,直接""能真,就會(huì)單獨(dú)顯示一個(gè)系統(tǒng)的返回箭頭圖標(biāo),也是很清晰的感覺逢勾。
做到這里發(fā)現(xiàn)文字顏色和背景有重復(fù),那么如何自定義其顏色呢藐吮?在iOS7溺拱,可以改變tintColor屬性,它提供了一個(gè)快速和簡單的方式谣辞,下面是一個(gè)示例代碼片段:
[objc] view plaincopy
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
效果如下:
全是系統(tǒng)的圖標(biāo)和文字迫摔,這回看著舒服了,有木有泥从?【除了后退按鈕句占,請(qǐng)注意,tintColor屬性影響所有按鈕標(biāo)題和按鈕圖像】
最后舉個(gè)例子躯嫉,另外一種實(shí)現(xiàn)自定義導(dǎo)航控制器返回按鈕纱烘,代碼如下:
[objc] view plaincopy
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:19.0]}];
self.title=[NSString stringWithFormat:@"第%lu頁",(unsigned long)self.navigationController.viewControllers.count];
//自定義返回按鈕
UIImage *backButtonImage = [[UIImage imageNamed:@"fanhui.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//將返回按鈕的文字position設(shè)置不在屏幕上顯示
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
最后說一下使用pushViewController切換到下一個(gè)視圖時(shí),navigation controller按照以下3條順序更改導(dǎo)航欄的左側(cè)按鈕(本段摘自網(wǎng)絡(luò)):
1祈餐、如果B視圖有一個(gè)自定義的左側(cè)按鈕(leftBarButtonItem)擂啥,則會(huì)顯示這個(gè)自定義按鈕;
2帆阳、如果B沒有自定義按鈕哺壶,但是A視圖的backBarButtonItem屬性有自定義項(xiàng),則顯示這個(gè)自定義項(xiàng)蜒谤;
3山宾、如果前2條都沒有,則默認(rèn)顯示一個(gè)后退按鈕鳍徽,后退按鈕的標(biāo)題是A視圖的標(biāo)題资锰;