InteractivePopGestureRecognizer弹出到根目录,而不是1个顶部控制器

时间:2019-06-06 17:46:52

标签: ios swift

我使用UINavigationController的默认实现和默认手势来左右弹出(interactivePopGestureRecognizer)。如何使interactivePopGestureRecognizer弹出到根控制器,而不是仅弹出1个顶部控制器?

1 个答案:

答案 0 :(得分:0)

我找到了一种方法,可以在推送一个新的viewController之后从堆栈中删除它。

navigationController.pushViewController(newViewController, animated: true, completion: {
    self.navigationController.removePreviousViewController()
})

这里是扩展名

extension UINavigationController {
    func pushViewController(_ viewController: UIViewController, animated: Bool, completion: @escaping () -> Void) {
        pushViewController(viewController, animated: animated)
        guard animated, let coordinator = transitionCoordinator else {
            DispatchQueue.main.async { completion() }
            return
        }
        coordinator.animate(alongsideTransition: nil) { _ in completion() }
    }

    func removePreviousViewController() {
        if viewControllers.count > 2 {
            viewControllers.removePrevious()
        }
    }
}

一些助手

extension Array {
    mutating func removePrevious() {
        remove(at: count - 2)
    }
}