iOS 7.0自定义导航栏后返回按钮失效(leftBarButtonItem使用后
)
两种解决办法
第一种(比较简单但有缺陷,缺陷稍后再说)
1、自定义导航栏遵守UINavigationBarDelegate协议
2、实现UINavigationBarDelegate的两个方法
自己最好建一个UINavigationController子类(我这里是CihonNavViewController)代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
__weak CihonNavViewController *weakSelf = self;
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
self.interactivePopGestureRecognizer.delegate = weakSelf;
}
// Do any additional setup after loading the view.
}
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
//只有一个控制器的时候禁止手势,防止卡死现象
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled = NO;
}
if (self.childViewControllers.count > 1) {
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled = YES;
}
}
return YES;
}
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item {
//只有一个控制器的时候禁止手势,防止卡死现象
if (self.childViewControllers.count == 1) {
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled = NO;
}
}
}
OK搞定,现在Run一下,发现问题已经解决了,他妈的但是来了。发现只有滑返回手势只能靠近屏幕左边缘才能使用
接下来说第二种解决办法
setp1:需要获取系统自带滑动手势的target对象id target = self.navigationController.interactivePopGestureRecognizer.delegate;
setp2:创建全屏滑动手势~调用系统自带滑动手势的target的action方法UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
step3:设置手势代理~拦截手势触发pan.delegate = self;
step4:别忘了~给导航控制器的view添加全屏滑动手势[self.view addGestureRecognizer:pan];
step5:将系统自带的滑动手势禁用
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
steo6:还记得刚刚设置的代理吗?下面方法什么时候调用?在每次触发手势之前都会询问下代理,是否触发。
这个方法就是拦截手势触发的方法.- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{}
return NO;则不需要触发滑动手势
return YES;则需要触发滑动手势
方法二参考文章如下