我使用UINavigationController
的默认实现和默认手势来左右弹出(interactivePopGestureRecognizer)。如何使interactivePopGestureRecognizer
弹出到根控制器,而不是仅弹出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)
}
}