简体   繁体   中英

interactivePopGestureRecognizer pop to root instead of 1 top controller

I use default implementation of UINavigationController with default gesture for popping left to right (interactivePopGestureRecognizer). How can I make interactivePopGestureRecognizer popping to root controller instead of only 1 top controller?

I found a way to just delete previous viewController from stack after pushing a new one.

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

A here is an extension

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()
        }
    }
}

Some helpers

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM