標籤:
原文網址:http://blog.csdn.net/mad1989/article/details/41516743
在iOS7下,預設導覽列背景,顏色是這樣的,接下來我們就進行自訂,如果你僅僅是更改一下背景和顏色,代碼會很簡單,不需要很複雜的自訂View來替代leftBarItem
更改導覽列的背景和文字Color
方法一:
[objc] view plain copy
- //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]];
效果如下:
我們把背景改成了藍色,title文字改成了白色,是不是很簡單呢?NavigationBar極其push過去的子頁面也會是你修改後的背景顏色
方法二:
[objc] view plain copy
- //設定NavigationBar背景顏色
- [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
- //@{}代表Dictionary
- [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
效果如下:
在導覽列使用背景圖片:
如果您的應用程式使用了自訂映像作為欄的背景,你需要提供一個“更大”的圖片,使其延伸了狀態列的後面。導覽列的高度現在是從44點(88像素)更改為64點(128像素)。
仍然可以使用了setBackgroundImage:方法來指定自訂映像的導覽列。下面是程式碼設定背景圖片:
[objc] view plain copy
- [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];
和上面的一樣,我就不貼出來了。
改變導覽列標題的字型
就像iOS 6,我們可以通過使用導覽列的“titleTextAttributes”屬性來自訂的文本樣式。可以指定字型,文字顏色,文字陰影顏色,文字陰影在文本標題位移屬性字典,使用下面的文字屬性鍵:
UITextAttributeFont - 字型
UITextAttributeTextColor - 文字顏色
UITextAttributeTextShadowColor - 文字陰影顏色
UITextAttributeTextShadowOffset - 位移用於文本陰影
[objc] view plain copy
- 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]];
使用圖片作為導覽列標題
不想標題列是光禿禿的文字?可以通過使用程式碼中的映像或標誌取代它:簡單地改變titleview用來自訂,(適用於較低版本)
[objc] view plain copy
- self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"appcoda-logo.png"]];
效果如下,我隨便用了個圖片,別介意:
添加多個欄按鈕項目
您希望添加導覽列的一側不止一個欄按鈕項目,無論是leftBarButtonItems和rightBarButtonItems 您在導覽列左側/右側指定自訂欄按鈕項目。比如你想添加一個網路攝影機和一個共用按鈕右側的吧。您可以使用下面的代碼:
[objc] view plain copy
- 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時,push到的子頁面,左上方會是系統自動取值上一層父頁面的title名稱,預設情況是這樣,那麼我們該如何修改它呢?
左側顯示了父頁面的title:使用者登入,可是我們想修改成返回,方式有很多,舉些例子
方法一:
通過設定navigationItem的backBarButtonItem可以直接更換文字,【注意,要在父視圖的Controller中設定】如下:
[objc] view plain copy
- UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
- self.navigationItem.backBarButtonItem = item;
效果如下:
所有的子介面返回時都變成了我們定義的文字,如果不想顯示文字,直接"",就會單獨顯示一個系統的返回箭頭表徵圖,也是很清晰的感覺。
做到這裡發現文字顏色和背景有重複,那麼如何自訂其顏色呢?在iOS7,可以改變tintColor屬性,它提供了一個快速和簡單的方式,下面是一個範例程式碼片段:
[objc] view plain copy
- [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
效果如下:
全是系統的表徵圖和文字,這回看著舒服了,有木有?【除了後退按鈕,請注意,tintColor屬性影響所有按鈕標題和按鈕映像】
最後舉個例子,另外一種實現自訂導航控制器返回按鈕,代碼如下:
[objc] view plain copy
- [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設定不在螢幕上顯示
- [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
效果如下:
最後說一下使用pushViewController切換到下一個視圖時,navigation controller按照以下3條順序更改導覽列的左側按鈕(本段摘自網路):
1、如果B視圖有一個自訂的左側按鈕(leftBarButtonItem),則會顯示這個自訂按鈕;
2、如果B沒有自訂按鈕,但是A視圖的backBarButtonItem屬性有自訂項,則顯示這個自訂項;
3、如果前2條都沒有,則預設顯示一個後退按鈕,後退按鈕的標題是A視圖的標題;
【轉】自訂iOS7導覽列背景,標題和返回按鈕文字顏色 -- 不錯不錯!!