iOS-自訂NavigationItem返回按鈕【pop返回按鈕】

來源:互聯網
上載者:User

標籤:selector   nta   hidden   重寫   dap   gif   action   color   圖片   

在用navigationVC時,返回按鈕有時候不想用系統的,這裡用繼承的方式把按鈕替換了,同時也可以實現系統的右滑返回,很簡單;

1.建立基類 BasePopViewController

建立一個用於替換系統返回按鈕基類的 BasePopViewController : UIViewController;

BasePopViewController.m

#import "BasePopViewController.h"@interface BasePopViewController ()@end@implementation BasePopViewController- (void)viewDidLoad {    [super viewDidLoad];    [self setNavigationItemBackButton];    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;}- (void)viewDidAppear:(BOOL)animated {    [super viewDidAppear:animated];    if (self.navigationController.viewControllers.count > 1) {        self.navigationController.interactivePopGestureRecognizer.enabled = YES;    }else{        self.navigationController.interactivePopGestureRecognizer.enabled = NO;    }}/** 自訂狀態列 */- (void)setNavigationItemBackButton{    if (self.navigationController.viewControllers.count > 1) {        UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back_off"] style:UIBarButtonItemStylePlain target:self action:@selector(popBackButtonAction)];        self.navigationItem.leftBarButtonItems = @[back];    }}/** 返回按鈕事件 */- (void)popBackButtonAction {    [self.navigationController popViewControllerAnimated:YES];}#pragma mark - 下面是設定的狀態列顏色,可忽略-(UIStatusBarStyle)preferredStatusBarStyle{    ///這裡設定白色    return UIStatusBarStyleLightContent;}-(BOOL)prefersStatusBarHidden{    return NO;}@end
2.引用

在需要替換系統的返回按鈕時,建立VC繼承BasePopViewController即可,如果要在新的VC中擷取點擊的返回按鈕事件,在新的VC中重寫 popBackButtonAction 事件即可。

 

 

iOS-自訂NavigationItem返回按鈕【pop返回按鈕】

相關文章

聯繫我們

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