第十三篇、Swift_Nav自定义返回按钮后或者隐藏导航栏,Pop返回手势失效的解决方法 Pop全局返回添加的方法
边缘的pop返回手势:
override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.purple // 保留Pop手势 navigationController?.interactivePopGestureRecognizer?.delegate = self navigationController?.interactivePopGestureRecognizer?.isEnabled = true }
这些代码主要实在UINavigationController上添加
Swift全屏pop返回手势:
class CustomNavigationController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() // 1.取出手势&view guard let gesture = interactivePopGestureRecognizer else { return } gesture.isEnabled = false let gestureView = gesture.view // 2.获取所有的target let target = (gesture.value(forKey: "_targets") as? [NSObject])?.first guard let transition = target?.value(forKey: "_target") else { return } let action = Selector(("handleNavigationTransition:")) // 3.创建新的手势 let popGes = UIPanGestureRecognizer() popGes.maximumNumberOfTouches = 1 gestureView?.addGestureRecognizer(popGes) popGes.addTarget(transition, action: action) } override func pushViewController(_ viewController: UIViewController, animated: Bool) { viewController.hidesBottomBarWhenPushed = true super.pushViewController(viewController, animated: animated) } }
OC POP手势返回:
- (void)viewDidLoad { [super viewDidLoad]; id target = self.interactivePopGestureRecognizer.delegate; UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:target action:@selector(handleNavigationTransition:)]; pan.delegate = self; [self.view addGestureRecognizer:pan]; [self.interactivePopGestureRecognizer setEnabled:NO]; } - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { return self.childViewControllers.count == 1 ? NO : YES; }