iOS 首頁側滑卡死的問題 解決方案

首先先說說這個卡死的bug是如何觸發(fā)的;

Navigation 是一個由一系列Controller組成的棧,棧的原理是先進后出;舉個例子:一摞書放在地上,如果你想要拿出最后一本,就要先把上面的書先拿掉;

Navigation同樣,假如這個棧中有多個Controller,

側滑手勢interactivePopGestureRecognizer會將最上面的一個Controller移出棧并銷毀頁面,

問題就在這里,如果這個Navigation的棧中只有一個Controller,通過側滑手勢移除后,棧中就沒有了這個Controller,才導致了頁面的卡死;

知道了這個導致這個問題的原因后,再想要解決這個問題就很簡單,我們只需要找到側滑手勢interactivePopGestureRecognizer監(jiān)聽事件,保證Navigation棧中至少有一個Controller;

首先你的NavigationController要有個父類我這里命名為BaseNavigationViewController,在父類中加要用到的代理

@interface BaseNavigationViewController () <UINavigationControllerDelegate,UIGestureRecognizerDelegate>

@end

設置代理

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? __weak BaseNavigationViewController *weakSelf = self;

? ? if([self respondsToSelector:@selector(interactivePopGestureRecognizer)])

? ? {

? ? ? ? self.interactivePopGestureRecognizer.delegate = weakSelf;

? ? ? ? self.delegate= weakSelf;

? ? }

}?

接著重寫NavigationControllerDelegate中展示頁面的方法

- (void)navigationController:(UINavigationController*)navigationController

?? ? ? didShowViewController:(UIViewController*)viewController

? ? ? ? ? ? ? ? ? ? animated:(BOOL)animate

{

? ? NSArray* ctrlArray = navigationController.viewControllers;

? ? if(ctrlArray.count>1) {

? ? ? ? if([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

? ? ? ? ? ? self.interactivePopGestureRecognizer.enabled = YES;

? ? ? ? }

? ? }else{

? ? ? ? if([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

? ? ? ? ? ? self.interactivePopGestureRecognizer.enabled = NO;

? ? ? ? }

? ? }

}

只要保證Navigation棧中至少有一個Controller即可

到這里問題就已經解決了

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容