從iOS7開始键思,系統(tǒng)為UINavigationController提供了一個interactivePopGestureRecognizer用于右滑返回(pop),但是,如果自定了當(dāng)前視圖控制器leftBarButtonItem,該手勢就失效了。
解決方法:自定義UINavigationController
馅而,實現(xiàn)其代理方法:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
如果push的是非根控制器包吝,設(shè)置self.interactivePopGestureRecognizer.delegate = nil;
。
如果是根控制器腹纳,將原來的代理重新設(shè)置即可痢掠。
具體代碼如下:
#import "LGNavigationController.h"
@interface LGNavigationController ()<UINavigationControllerDelegate>
@property (nonatomic,strong) id popDelegate;
@end
- (void)viewDidLoad {
[super viewDidLoad];
//代理
self.popDelegate = self.interactivePopGestureRecognizer.delegate;
self.delegate = self;
}
#pragma UINavigationControllerDelegate方法
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//實現(xiàn)滑動返回功能
//清空滑動返回手勢的代理就能實現(xiàn)
self.interactivePopGestureRecognizer.delegate = viewController == self.viewControllers[0]? self.popDelegate : nil;
}