IOS 改變Navigation的返回按鈕

來源:互聯網
上載者:User

標籤:style   http   color   使用   strong   os   

兩個辦法:

1, 手動為每一個UIViewController添加navigationItem的leftButton的設定代碼

2,為UINavigationController實現delegate,在pop和push的時候改變當前和上一頁的navigationItem.title

以下是封裝的一些基礎方法,供參考:

+ (void) navigationItem:(UINavigationItem*)navigationItem 

               setTitle:(NSString*)title;

+ (void) navigationItem:(UINavigationItem*)navigationItem

          setBackButton:(NSString*)title

              addTarget:(id)target

                 action:(SEL)action;

+ (void) navigationItem:(UINavigationItem*)navigationItem 

               setImage:(NSString*)imageString;

無奈之下,只好研讀UINavigationController Class Reference去,在“Updating the Navigation Bar”小節,有這麼一段話:

The bar button item on the left side of the navigation bar allows for navigation back to the previous view controller on the navigation stack. The navigation controller updates the left side of the navigation bar as follows:

  • If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.

  • If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has a valid item in its backBarButtonItemproperty, the navigation bar displays that item.

  • If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack. (If there is only one view controller on the navigation stack, no back button is displayed.)

我大致解釋一下,使用pushViewController切換到下一個視圖時,navigation controller按照以下3條順序更改導覽列的左側按鈕。

1、如果B視圖有一個自訂的左側按鈕(leftBarButtonItem),則會顯示這個自訂按鈕;

2、如果B沒有自訂按鈕,但是A視圖的backBarButtonItem屬性有自訂項,則顯示這個自訂項;

3、如果前2條都沒有,則預設顯示一個後退按鈕,後退按鈕的標題是A視圖的標題。

按照這個解釋,我把UIBarButtonItem *backItem……這段代碼放在A視圖的pushViewController語句之前。

OK問題解決了,B視圖的後退按鈕的標題變成back了。

eg:

UIBarButtonItem*backItem=[[UIBarButtonItemalloc]init];

    [email protected]"戻る";

    self.navigationItem.backBarButtonItem=backItem;

    [backItem release]

==============

新方案:

self.navigationController.navigationBar.topItem.title=self.message;

    self.navigationController.navigationBar.tintColor=[UIColorblackColor];

    UIBarButtonItem*backButton = [[UIBarButtonItemalloc] initWithTitle:@" fan hui "style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(PopViewController)];

    self.navigationItem.leftBarButtonItem= backButton;

以上轉自:http://hi.baidu.com/myyuchong/item/1e6db306c5d34de1359902a1

PS:在第三種情況的時候,自動建立的返回按鈕只取了A視圖的title值,其他屬性並沒有複製,看來要改變顏色等別的屬性還得自訂一個返回按鈕啊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.