How to disable back gesture in iOS 7 for only one view

后端 未结 6 1588
自闭症患者
自闭症患者 2020-12-18 06:26

I am trying to disable the back gesture for my view controller using the following set of code.

In FirstViewController.m, I\'m setting the delegate of <

6条回答
  •  一整个雨季
    2020-12-18 07:27

    I originally put these answers into a comment below the accepted answer, but I feel this needs to be said as an answer to get more visibility.

    More often than not, you will find that the accepted answer does not work. This is because viewWillAppear: can be called before the view is added to a navigation controller's view hierarchy, and so self.navigationController is going to be nil. Because of this, the interactivePopGestureRecognizer may not be disabled in some cases. You're better off calling it in viewDidAppear: instead.

    Here's code that will work (assuming your view controller is correctly added to a navigation controller's view hierarchy):

    Objective-C

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        [[[self navigationController] interactivePopGestureRecognizer] setEnabled:NO];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [[[self navigationController] interactivePopGestureRecognizer] setEnabled:YES];
    }
    

    Swift

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
    

提交回复
热议问题