带有自定义后退按钮的滑动手势冻结根视图控制器 [英] Slide gesture with custom back button freezes the root view controller
问题描述
我的应用程序中都有自定义后退按钮,看起来导航控制器不喜欢它.
I have custom back buttons all over my app and it looks like navigation controller does not like it.
所以,我希望 iOS7 滑动返回手势与我的自定义返回按钮一起使用.搜索并尝试了不同的方法,但似乎都没有希望.我能得到的最接近的是 http://keighl.com/post/ios7-interactive-pop-gesture-custom-back-button/.但是,现在当我继续推送和弹出导航堆栈时,堆栈中的 rootViewController 会停止响应任何触摸.
So, I want the iOS7 swipe-to-go-back gesture to work along with my custom back buttons. Searched and tried different ways but none seems to be promising. The closest I could get is with http://keighl.com/post/ios7-interactive-pop-gesture-custom-back-button/. However, Now when I keep pushing and popping the navigation stack, after sometime the rootViewController in the stack stops responding to any touch.
有什么建议吗?
推荐答案
继承 UINavigationController,就像 keighl 建议的那样,是 imo 的正确方法.但是他错过了对根视图控制器的检查,以避免在根视图上执行手势时冻结.这是带有附加检查的修改版本:
Subclassing UINavigationController, like keighl is suggesting, is the right approach imo. But he's missing a check for the root view controller to avoid freezing when the gesture is executed on the root view. Here's a modified version with the additional check:
CBNavigationController.h:
CBNavigationController.h:
#import <UIKit/UIKit.h>
@interface CBNavigationController : UINavigationController <UIGestureRecognizerDelegate, UINavigationControllerDelegate>
@end
CBNavigationController.m:
CBNavigationController.m:
#import "CBNavigationController.h"
@interface CBNavigationController ()
@end
@implementation CBNavigationController
- (void)viewDidLoad
{
NSLog(@"%s",__FUNCTION__);
__weak CBNavigationController *weakSelf = self;
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
self.interactivePopGestureRecognizer.delegate = weakSelf;
self.delegate = weakSelf;
}
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
NSLog(@"%s",__FUNCTION__);
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = NO;
[super pushViewController:viewController animated:animated];
}
#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animate
{
NSLog(@"%s",__FUNCTION__);
// Enable the gesture again once the new controller is shown AND is not the root view controller
if (viewController == self.viewControllers.firstObject)
{
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = NO;
}
else
{
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = YES;
}
}
@end
这篇关于带有自定义后退按钮的滑动手势冻结根视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!