iOS7+自訂返回按鈕

來源:互聯網
上載者:User

標籤:

本來我是用建立UIButton賦值給self.navigationItem.leftBarButtonItem的方式設定返回按鈕的,用了覺得挺正常,結果看了@蛙牛的文章《Java轉iOS-第一個項目總結(2)》後,發現一個重大問題:手勢返回功能沒了,原因就是用設定leftBarButtonItem的方式來自訂返回按鈕會導致手勢返回功能失效。

如果不自訂返回按鈕,又因為我使用的是RDVTabBarController,需要在返回方法裏手動設定hide tabbar屬性,這樣我就需要攔截返回手勢的觸發方法,想起來頭都大了。

摸索了半天,找到了一個簡單自訂返回按鈕並且保留手勢返回功能的方法,最佳化一下hide tabbar的方式就可以不用攔截了。在BaseViewController中定義一個isShowTabBar,預設為NO,在一級頁面的init或者initWithNibName方法中設定isShowTabBar=YES,其它的ViewController不用動,BaseViewController重寫下面的方法:

- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    if (self.rdv_tabBarController.isTabBarHidden == isShowTabbar) {        [self.rdv_tabBarController setTabBarHidden:!isShowTabbar];    }}

自訂不帶文字的返回按鈕代碼如下:(代碼來自於自訂iOS7導覽列背景,標題和返回按鈕文字顏色)

//自訂返回按鈕  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];

顯示效果如下:



iOS7+自訂返回按鈕

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.