- 主要实现原理
@implementation UINavigationController (ShouldPopOnBackBtn)
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
BOOL shouldPop = YES;
// 看一下当前控制器有没有实现代理方法 currentViewControllerShouldPop
// 如果实现了,根据当前控制器的代理方法的返回值决定
// 没过没有实现 shouldPop = YES
UIViewController* currentVC = [self topViewController];
if([currentVC respondsToSelector:@selector(currentViewControllerShouldPop)]) {
shouldPop = [currentVC currentViewControllerShouldPop];
}
if(shouldPop)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self popViewControllerAnimated:YES];
});
// 这里要return, 否则这个方法将会被再次调用
return YES;
}
else
{
// 让系统backIndicator 按钮透明度恢复为1
for(UIView *subview in [navigationBar subviews]) {
if(0. < subview.alpha && subview.alpha < 1.) {
[UIView animateWithDuration:.25 animations:^{
subview.alpha = 1.;
}];
}
}
return NO;
}
}
@end
- 如何使用
// 第一步:导入分类头文件
#import "UIViewController+BackBtnEventIntercept.h"
// 第二步:实现代理方法,return NO 则拦截了系统的返回按钮事件
- (BOOL)currentViewControllerShouldPop
{
return NO;
}
- 如何禁用系统👉右滑返回手势
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// 为当前控制器禁用👉右滑返回手势
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// 为其他控制器开启👉右滑返回手势
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
- 如何使用了 FDFullscreenPopGesture,则使用下面的方式禁止右划
self.fd_interactivePopDisabled = YES;
强烈推荐:超简单!!! iOS设置状态栏、导航栏按钮、标题、颜色、透明度,偏移等
https://github.com/wangrui460/WRNavigationBar
https://github.com/wangrui460/WRNavigationBar_swift
欢迎关注我的微博:wangrui460