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

标签 ios swift

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

相关文章:

ios - 拦截 UIPageViewController 的 UIPanGestureRecognizer 会破坏 UIPageViewController 的行为

ios - ABPeoplePickerNavigationController 从联系人 Swift 获取图像

swift - 更新到 Xcode 8.3 后 UIButton 不工作

ios - 当设备方向改变时改变 ViewController?

ios - 通过ID从数组中提取文本

ios - 带有 GradientLayer 的 UIButton 会遮挡图像并使渐变变暗

ios - 如何在 CloudKit 仪表板中使用 "fetchWithRecordID"自动生成 ID?

ios - webmethod 在 ios 调用上接收空参数

ios - ScrollView 内的 SwiftUI 列表

swift - 是否可以保存来自不同 View Controller 的数据并将其显示在同一个 TableView 单元中?