使用UINavigationController遇到的坑

遇到的问题:

频繁跳转页面,偶尔出现界面卡死,不响应任何手势,点击事件

原因:

在视图Push过程中,且Push尚未完成时触发了Pop,可能会导致界面卡死,不响应任何手势,点击事件

解决方法:

重写Push事件,在Push过程中禁用其他操作,包括滑动返回手势

#import "MyNavigationViewController.h"

@interface MyNavigationViewController ()
@property (assign, nonatomic) BOOL isSwitching;

@end

@implementation MyNavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.delegate = self;
    self.interactivePopGestureRecognizer.delegate = self;

}
/**
 *  重写push方法
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    self.interactivePopGestureRecognizer.enabled = NO;

    if (animated) {
        if (self.isSwitching) {
            return; // 1. 如果是动画,并且正在切换,直接忽略
        }
        self.isSwitching = YES; // 2. 否则修改状态
    }
    
    // 所有设置搞定后, 再push控制器
    [super pushViewController:viewController animated:animated];
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    self.interactivePopGestureRecognizer.enabled = [self.viewControllers count] > 1 ; 

    self.isSwitching = NO; // 3. 还原状态
    
}
@end

你可能感兴趣的:(使用UINavigationController遇到的坑)