Navigation pop view when swipe right like Instagram iPhone app.How i achieve this?

前端 未结 5 880
有刺的猬
有刺的猬 2020-12-02 10:48

I want to pop a view when swipe right on screen or it\'s work like back button of navigation bar.

I am using:

self.navigationController.interactivePo         


        
5条回答
  •  感动是毒
    2020-12-02 11:16

    There really is no need to roll your own solution for this, sub-classing UINavigationController and referencing the built-in gesture works just fine as explained here.

    The same solution in Swift:

    public final class MyNavigationController: UINavigationController {
    
      public override func viewDidLoad() {
        super.viewDidLoad()
    
    
        self.view.addGestureRecognizer(self.fullScreenPanGestureRecognizer)
      }
    
      private lazy var fullScreenPanGestureRecognizer: UIPanGestureRecognizer = {
        let gestureRecognizer = UIPanGestureRecognizer()
    
        if let cachedInteractionController = self.value(forKey: "_cachedInteractionController") as? NSObject {
          let string = "handleNavigationTransition:"
          let selector = Selector(string)
          if cachedInteractionController.responds(to: selector) {
            gestureRecognizer.addTarget(cachedInteractionController, action: selector)
          }
        }
    
        return gestureRecognizer
      }()
    }
    

    If you do this, also implement the following UINavigationControllerDelegate function to avoid strange behaviour at the root view controller:

    public func navigationController(_: UINavigationController,
                                     didShow _: UIViewController, animated _: Bool) {
      self.fullScreenPanGestureRecognizer.isEnabled = self.viewControllers.count > 1
    }
    

提交回复
热议问题