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

自古美人都是妖i 提交于 2019-11-29 07:34:47

Try the below untested code in your FirstViewController :

-(void) viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

-(void) viewWillDisappear:(BOOL)animated 
{
    [super viewWillDisappear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}

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
}

I tried the all above but they did not work for me.So i tried this and it works for me on both IOS7 and IOS8.

Just make sure that your view controller implements this protocol i.e UIGestureRecognizerDelegate and write the code given below.

-(void)viewWillAppear : (BOOL) animated {

[super viewWillAppear : animated];

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

    self.navigationController.interactivePopGestureRecognizer.enabled =

NO;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) {

    return NO;

} else {

    return YES;

}

}

I found out setting the gesture to disabled only doesn't always work. It does work, but for me it only did after I once used the backgesture. Second time it wouldn't trigger the backgesture. Furthermore, as John Rogers said, it's import to use the viewDidAppear and viewWillAppear as the navigationController else would be nil.

Fix for me was to delegate the gesture and implement the shouldbegin method to return NO:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Disable iOS 7 back gesture
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    // Enable iOS 7 back gesture
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return NO;
}

This just worked for me in xCode 7:

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController!.interactivePopGestureRecognizer!.enabled = false

}
override func viewWillDisappear(animated: Bool) {
    super.viewDidDisappear(animated)
    self.navigationController!.interactivePopGestureRecognizer!.enabled = true
}

For only one view, I don't know the way... But I use the next code to disable fully the swipe gesture:

in your AppDelegate.m

if ([[UIDevice currentDevice].systemVersion floatValue] >= 7){
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!