修改系统的 返回按钮文字,获取系统返回按钮的点击事件

一.修改系统返回按钮文字 并且保留系统的左滑返回手势,
左滑手势的拦截,结合UINavigationController+FDFullscreenPopGesture这个分类

//在需要push的地方修改self.navigationItem.backBarButtonItem的样式就可以了
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NextViewController *vc = [NextViewController new];
    UIBarButtonItem *back = [[UIBarButtonItem alloc]init];
    back.title = @"";
    self.navigationItem.backBarButtonItem = back;
    [self.navigationController pushViewController:vc animated:YES];
}

二.获取系统返回按钮的点击事件
1.创建UIViewController 的category ,在分类上添加Protocol,并让分类遵循这个协议
//.h 文件
UIViewController+BackButtonHandler.h

#import <UIKit/UIKit.h>

@protocol BackButtonHandlerProtocol <NSObject>
@optional
-(BOOL)navigationShouldPopOnBackButton;
@end

@interface UIViewController (BackButtonHandler) <BackButtonHandlerProtocol>

@end

//.m 文件
UIViewController+BackButtonHandler.m

#import "UIViewController+BackButtonHandler.h"

@implementation UIViewController (BackButtonHandler)

@end

@implementation UINavigationController (ShouldPopOnBackButton)

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {

    if([self.viewControllers count] < [navigationBar.items count]) {
        return YES;
    }

    BOOL shouldPop = YES;
    UIViewController* vc = [self topViewController];
    if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
        shouldPop = [vc navigationShouldPopOnBackButton];
    }

    if(shouldPop) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self popViewControllerAnimated:YES];
        });
    } else {
        for(UIView *subview in [navigationBar subviews]) {
            if(0. < subview.alpha && subview.alpha < 1.) {
                [UIView animateWithDuration:.25 animations:^{
                    subview.alpha = 1.;
                }];
            }
        }
    }

    return NO;
}

@end

2.在ViewController 里面直接导入

import "UIViewController+BackButtonHandler.h"头文件

在ViewController 里面直接实现 protocol, 也可以不实现,默认是返回YES
-(BOOL)navigationShouldPopOnBackButton
{
//这里可以实现你想做的事
   return YES; //YES就是返回,NO就是不返回
}

项目地址:https://github.com/xinqin2496/popBackDemo

推荐阅读更多精彩内容