// LZNavigationController.h
#import <UIKit/UIKit.h>
@interface LZNavigationController : UINavigationController
@end
// LZNavigationController.m
#import "LZNavigationController.h"
@interface LZNavigationController () <UIGestureRecognizerDelegate>
@end
@implementation LZNavigationController
+ (void)initialize
{
// 1.拿到导航条
UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
// 2.目的把导航条背景图片
[bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
NSMutableDictionary * attr = [NSMutableDictionary dictionary];
attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
[bar setTitleTextAttributes:attr];
}
- (void)viewDidLoad {
[super viewDidLoad];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
// 把手势添加到view上
[self.view addGestureRecognizer:pan];
pan.delegate = self;
// 尝试了一下,感觉下面这行代理没有什么用,不过最好不要省略,万一出现什么bug呢
self.interactivePopGestureRecognizer.enabled = NO;
}
#pragma mark - UIGestureRecognizerDelegate方法
// 是否触发手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// 根控制器下不要触发手势,让手势不起作用
return self.childViewControllers.count > 1 ;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// 滑动返回功能为什么失效:用滑动手势做,验证滑动手势是否还在(验证之后还在)
// 代理可以控制手势是否有效,验证:代理做了一些事情,导致滑动手势失效
/**
* self.interactivePopGestureRecognizer =
<UIScreenEdgePanGestureRecognizer: 0x7fa2d3c5c820; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7fa2d3ea5c60>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7fa2d3c5c270>)>>
self.interactivePopGestureRecognizer.delegate =
<_UINavigationInteractiveTransition: 0x7fa7c372a850> 滑动手势代理
*/
if (self.childViewControllers.count != 0) { // 非根控制器
viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(back) image:@"navigationButtonReturn" highImage:@"navigationButtonReturnClick" color:[UIColor blackColor] highColor:[UIColor redColor] title:@"返回"];
self.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:YES];
}
- (void)back
{
[self popViewControllerAnimated:YES];
}
@end