wangrui460/WRNavigationBar

ios13 点返回按钮闪退问题

bingolib opened this issue · 14 comments

  • (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 13.0) {
    return YES;
    }else{
    __weak typeof (self) weakSelf = self;
    id coor = [self.topViewController transitionCoordinator];
    if ([coor initiallyInteractive] == YES) {
    NSString *sysVersion = [[UIDevice currentDevice] systemVersion];
    if ([sysVersion floatValue] >= 10) {
    if (@available(iOS 10.0, *)) {
    [coor notifyWhenInteractionChangesUsingBlock:^(id _Nonnull context) {
    __strong typeof (self) pThis = weakSelf;
    [pThis dealInteractionChanges:context];
    }];
    } else {
    // Fallback on earlier versions
    }
    } else {
    [coor notifyWhenInteractionEndsUsingBlock:^(id _Nonnull context) {
    __strong typeof (self) pThis = weakSelf;
    [pThis dealInteractionChanges:context];
    }];
    }
    return YES;
    }

      NSUInteger itemCount = self.navigationBar.items.count;
      NSUInteger n = self.viewControllers.count >= itemCount ? 2 : 1;
      UIViewController *popToVC = self.viewControllers[self.viewControllers.count - n];
      [self popToViewController:popToVC animated:YES];
      return YES;
    

    }
    }

同问

请问楼上的解决了么?

同问

在iOS 13上,[self popViewControllerAnimated:YES]; 这句就不要执行了,可以这么写:
if (!@available(iOS 13.0, *)) { [self popViewControllerAnimated:YES]; }

IIIOS commented

同问

pop放主线程即可

//    WRNavigationBar.m
//   - (BOOL)navigationBar: shouldPopItem: 方法
dispatch_async(dispatch_get_main_queue(), ^{
        [self popToViewController:popToVC animated:YES];
});

pop放主线程即可

//    WRNavigationBar.m
//   - (BOOL)navigationBar: shouldPopItem: 方法
dispatch_async(dispatch_get_main_queue(), ^{
        [self popToViewController:popToVC animated:YES];
});

确实好使,非常感谢

有没有彻底解决这个问题的?

有 全局搜索 - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item 这个方法 iOS 13需要单独在处理 如果没有特殊需求 直接返回yes

`- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {

if (@available(iOS 13.0, *)) {
    return YES;
}
id<UIViewControllerTransitionCoordinator> transitionCoordinator = self.topViewController.transitionCoordinator;
if (transitionCoordinator && transitionCoordinator.initiallyInteractive) {
    // 滑动手势
    if (@available(iOS 10.0, *)) {
        [transitionCoordinator notifyWhenInteractionChangesUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
            [self dealInteractionChangesWithContext:context];
        }];
    } else {
        [transitionCoordinator notifyWhenInteractionChangesUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
            [self dealInteractionChangesWithContext:context];
        }];
    }
} else {
    
    
    
    
    BOOL shouldPop = YES;
    UIViewController* vc = [self topViewController];
    if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
        shouldPop = [vc navigationShouldPopOnBackButton];
        if(shouldPop) {
            NSInteger idx = self.viewControllers.count >= self.navigationBar.items.count ? 2 : 1;
            UIViewController *popToVC = self.viewControllers[self.viewControllers.count - idx];
            
            [self popToViewController:popToVC animated:YES];
        }else{
            return NO;
        }
    }else{
        NSInteger idx = self.viewControllers.count >= self.navigationBar.items.count ? 2 : 1;
        UIViewController *popToVC = self.viewControllers[self.viewControllers.count - idx];
        
        [self popToViewController:popToVC animated:YES];
    }
    
}
return YES;

}`

第一种可以用...

pop放主线程即可

//    WRNavigationBar.m
//   - (BOOL)navigationBar: shouldPopItem: 方法
dispatch_async(dispatch_get_main_queue(), ^{
        [self popToViewController:popToVC animated:YES];
});

这样做的具体原因是什么

FireG commented

pop放主线程即可

//    WRNavigationBar.m
//   - (BOOL)navigationBar: shouldPopItem: 方法
dispatch_async(dispatch_get_main_queue(), ^{
        [self popToViewController:popToVC animated:YES];
});

为什么这样做

感谢dispatch_async