gpt4 book ai didi

ios - interactivePopGestureRecognizer 弹出到根而不是 1 个顶部 Controller

转载 作者:行者123 更新时间:2023-11-28 07:26:27 26 4
gpt4 key购买 nike

我使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com