起初為了滿足設(shè)計(jì)要求,自定義了導(dǎo)航欄的self.navigationItem.leftBarButtonItem
然后發(fā)現(xiàn)UINavigationController自帶的側(cè)滑返回消失了澳叉;
于是我以為這很簡單衙四,遵守UIGestureRecognizerDelegate協(xié)議相种,
然后:
self.navigationController.interactivePopGestureRecognizer.delegate=self;
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
這樣解決了側(cè)滑返回的問題塑猖,本以為問題就這樣簡單搞定了,沒想到出了一個(gè)讓我頭疼好幾天才找出原因的bug擅编,簡直就是自己給自己挖了一個(gè)大坑攀细。
在rootViewController側(cè)滑后,程序就開始出現(xiàn)莫名其妙的bug爱态,點(diǎn)擊按鈕無法觸發(fā)事件谭贪,或者反應(yīng)很長時(shí)間,點(diǎn)一下锦担,卡半天俭识,最終卡死不動(dòng)。
找過很多地方的問題洞渔,都不能解決這個(gè)問題套媚。最終找到一個(gè)方法,自定義一個(gè)navigationController作為rootViewController磁椒。
代碼如下:
需要遵守:UINavigationControllerDelegate,UIGestureRecognizerDelegate協(xié)議
- (void)viewDidLoad {
[superviewDidLoad];
self.delegate=self;
__weaktypeof(self)weakSelf =self;
if([selfrespondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled=YES;
self.interactivePopGestureRecognizer.delegate= weakSelf;
}
}
在推出新視圖之前停用手勢堤瘤。
-(void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated{
if([selfrespondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.interactivePopGestureRecognizer.enabled=NO;
}
[superpushViewController:viewControlleranimated:animated];
}
新視圖加載完成后啟用手勢
-(void)navigationController:(UINavigationController*)navigationController didShowViewController:(UIViewController*)viewController animated:(BOOL)animated{
if([navigationControllerrespondsToSelector:@selector(interactivePopGestureRecognizer)]) {
navigationController.interactivePopGestureRecognizer.enabled=YES;
}
一般根控制器都是禁止右滑返回的,所以設(shè)置第一個(gè)控制器停用手勢浆熔。
if(navigationController.viewControllers.count==1) {
navigationController.interactivePopGestureRecognizer.enabled=NO;
navigationController.interactivePopGestureRecognizer.delegate=nil;
}
}
如果暫時(shí)理解不了的話本辐,照搬就行,能解決問題蘸拔,我也是打算把代碼存在這里以后常翻一下师郑。如果發(fā)現(xiàn)有不足的地方环葵,請?jiān)谠u(píng)論里指正调窍,謝謝~!