在用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 事件即可。