No Swipe Back when hiding Navigation Bar in UINavigationController

前端 未结 18 733
生来不讨喜
生来不讨喜 2020-12-04 06:34

I love the swipe pack thats inherited from embedding your views in a UINavigationController. Unfortunately i cannot seem to find a way to hide the Naviga

18条回答
  •  广开言路
    2020-12-04 06:44

    Looks like solution provided by @ChrisVasseli is the best. I'd like to provide same solution in Objective-C because question is about Objective-C (see tags)

    @interface InteractivePopGestureDelegate : NSObject 
    
    @property (nonatomic, weak) UINavigationController *navigationController;
    @property (nonatomic, weak) id originalDelegate;
    
    @end
    
    @implementation InteractivePopGestureDelegate
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        if (self.navigationController.navigationBarHidden && self.navigationController.viewControllers.count > 1) {
            return YES;
        } else {
            return [self.originalDelegate gestureRecognizer:gestureRecognizer shouldReceiveTouch:touch];
        }
    }
    
    - (BOOL)respondsToSelector:(SEL)aSelector
    {
        if (aSelector == @selector(gestureRecognizer:shouldReceiveTouch:)) {
            return YES;
        } else {
            return [self.originalDelegate respondsToSelector:aSelector];
        }
    }
    
    - (id)forwardingTargetForSelector:(SEL)aSelector
    {
        return self.originalDelegate;
    }
    
    @end
    
    @interface NavigationController ()
    
    @property (nonatomic) InteractivePopGestureDelegate *interactivePopGestureDelegate;
    
    @end
    
    @implementation NavigationController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.interactivePopGestureDelegate = [InteractivePopGestureDelegate new];
        self.interactivePopGestureDelegate.navigationController = self;
        self.interactivePopGestureDelegate.originalDelegate = self.interactivePopGestureRecognizer.delegate;
        self.interactivePopGestureRecognizer.delegate = self.interactivePopGestureDelegate;
    }
    
    @end
    

提交回复
热议问题