Disable swipe back gesture in Swift

后端 未结 10 1450
轻奢々
轻奢々 2020-12-02 09:47

Been looking around on here for a while but can\'t seem to find a working solution.

I\'m trying to disable the swipe to go back to previous view gesture, in Swift.

相关标签:
10条回答
  • 2020-12-02 10:11

    You could disable it but that would not be to recommended as most iOS users go back by swiping and less by pressing the back button. If you want to disable it it would be more reasonable to use a modal segue instead of a push segue which is not that big of a transfer. If you really want to get rid of the swipe to go back function I would just disable the back button and have a done button on the top right of the screen.

    self.navigationController?.navigationItem.backBarButtonItem?.isEnabled = false;
    
    0 讨论(0)
  • 2020-12-02 10:13

    The following is an easy approach to disabling & re-enabling the swipe back.

    Swift 3.x & up

    In a viewDidLoad/willAppear/didAppear method add:

    navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    

    Just keep in mind that if you do it with viewDidLoad, then the next time you open the view, it may not be set depending upon whether or not it remains in your stack.

    Unless you want it to remain off, you will need to turn it back on when the view is closed via either willMove(toParentViewController:) or willDisappear. Your navigationController will be nil at viewDidDisappear, so that is too late.

    navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    

    A special note on SplitViewControllers:

    As pointed out by CompC in the comments, you will need to call the second navigation controller to apply it to a detail view as such:

    navigationController?.navigationController?.interactivePopGe‌​stureRecognizer?.isE‌​nabled = false
    

    Swift 2.2 & Objective-C

    Swift versions 2.x & below:

    navigationController?.interactivePopGestureRecognizer?.enabled
    

    Objective-C:

    self.navigationController.interactivePopGestureRecognizer.enabled
    
    0 讨论(0)
  • 2020-12-02 10:18

    Add this line before pushing view controller to navigation controller

    self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    
    0 讨论(0)
  • 2020-12-02 10:18

    I generally make sure that swipe back is enabled in as many places as possible, even adding a custom gesture recognizer to add it to modal screens. However for an authentication and download process in my app I start the process with a modal navigation controller and then push the view for each next step. However, once it's completed I want to prevent them from backing up into the authentication screens.

    For this scenario I've been using:

    navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    navigationItem.hidesBackButton = true
    

    in viewWillAppear() on the final screen. You can undo these in viewWillDisappear() if you're pushing another view and need them there.

    0 讨论(0)
  • 2020-12-02 10:20

    Nothing wrong with either answer from Hari or Stefan but this is more succinct. Just put it in viewDidLoad and you're done.

    if navigationController!.respondsToSelector(Selector("interactivePopGestureRecognizer")) {
        navigationController!.view.removeGestureRecognizer(navigationController!.interactivePopGestureRecognizer)
    }
    

    EDIT:

    One small caveat is that if the Navigation Controller was opened by another view and the Navigation Controller is closed then you'll get an EXC_BAD_ACCESS error. To fix it you have to save the original UIGestureRecognizer and put it back when you exit the view.

    Declare:

    private var popGesture: UIGestureRecognizer?
    

    Immediately before removing the gesture:

    popGesture = navigationController!.interactivePopGestureRecognizer
    

    Then when closing the view:

    If popGesture != nil {
        navigationController!.view.addGestureRecognizer(popGesture!)
    }
    
    0 讨论(0)
  • 2020-12-02 10:23

    for objective -c

    -(void)viewWillAppear:(BOOL)animated{
      [super viewWillAppear:true];
    
      self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
    }
    
    0 讨论(0)
提交回复
热议问题