我使用 UINavigationController
的默认实现和从左到右弹出的默认手势 (interactivePopGestureRecognizer)。我怎样才能让 interactivePopGestureRecognizer
弹出到根 Controller 而不是只有 1 个顶级 Controller ?
最佳答案
我找到了一种方法,可以在推送新 View Controller 后从堆栈中删除之前的 View Controller 。
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()
}
}
}
一些 helper
extension Array {
mutating func removePrevious() {
remove(at: count - 2)
}
}
关于ios - interactivePopGestureRecognizer 弹出到根而不是 1 个顶部 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56482679/