From 81d8b69d61934e4702d59d531ce84294c9b92c55 Mon Sep 17 00:00:00 2001 From: Arman Dezfuli-Arjomandi Date: Mon, 11 Mar 2019 03:24:28 -0700 Subject: [PATCH] [V2] Fix iOS pop gesture when topBar is hidden (#4568) * Fix iOS pop gesture when nav bar is hidden * Add missing property * Factor out InteractivePopGestureDelegate into its own file * Add missing import * Make sure fix supports hidden and visible nav bars * Minor code style fix --- lib/ios/InteractivePopGestureDelegate.h | 20 +++++++++++ lib/ios/InteractivePopGestureDelegate.m | 35 +++++++++++++++++++ lib/ios/RNNNavigationController.m | 1 + lib/ios/RNNNavigationControllerPresenter.h | 3 ++ lib/ios/RNNNavigationControllerPresenter.m | 5 +++ .../project.pbxproj | 8 +++++ 6 files changed, 72 insertions(+) create mode 100644 lib/ios/InteractivePopGestureDelegate.h create mode 100644 lib/ios/InteractivePopGestureDelegate.m diff --git a/lib/ios/InteractivePopGestureDelegate.h b/lib/ios/InteractivePopGestureDelegate.h new file mode 100644 index 00000000000..db010bb4fb3 --- /dev/null +++ b/lib/ios/InteractivePopGestureDelegate.h @@ -0,0 +1,20 @@ +// +// InteractivePopGestureDelegate.h +// ReactNativeNavigation +// +// Created by Arman Dezfuli-Arjomandi on 1/10/19. +// Copyright © 2019 Wix. All rights reserved. +// +// + +// This file is adapted from the following StackOverflow answer: +// https://stackoverflow.com/questions/24710258/no-swipe-back-when-hiding-navigation-bar-in-uinavigationcontroller/41895151#41895151 + +#import + +@interface InteractivePopGestureDelegate : NSObject + +@property (nonatomic, weak) UINavigationController *navigationController; +@property (nonatomic, weak) id originalDelegate; + +@end diff --git a/lib/ios/InteractivePopGestureDelegate.m b/lib/ios/InteractivePopGestureDelegate.m new file mode 100644 index 00000000000..fd837633198 --- /dev/null +++ b/lib/ios/InteractivePopGestureDelegate.m @@ -0,0 +1,35 @@ +// +// InteractivePopGestureDelegate.m +// ReactNativeNavigation +// +// Created by Arman Dezfuli-Arjomandi on 1/10/19. +// Copyright © 2019 Wix. All rights reserved. +// + +#import "InteractivePopGestureDelegate.h" + +@implementation InteractivePopGestureDelegate + +- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { + if (self.navigationController.navigationBarHidden && self.navigationController.viewControllers.count > 1) { + return YES; + } else if (!self.navigationController.navigationBarHidden && self.originalDelegate == nil) { + 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 diff --git a/lib/ios/RNNNavigationController.m b/lib/ios/RNNNavigationController.m index 016d85d92ea..a316f35ad7f 100644 --- a/lib/ios/RNNNavigationController.m +++ b/lib/ios/RNNNavigationController.m @@ -2,6 +2,7 @@ #import "RNNNavigationController.h" #import "RNNModalAnimation.h" #import "RNNRootViewController.h" +#import "InteractivePopGestureDelegate.h" const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803; diff --git a/lib/ios/RNNNavigationControllerPresenter.h b/lib/ios/RNNNavigationControllerPresenter.h index e472d5ddc1e..21aa7aefdc7 100644 --- a/lib/ios/RNNNavigationControllerPresenter.h +++ b/lib/ios/RNNNavigationControllerPresenter.h @@ -1,9 +1,12 @@ #import "RNNBasePresenter.h" #import "RNNRootViewCreator.h" #import "RNNReactComponentRegistry.h" +#import "InteractivePopGestureDelegate.h" @interface RNNNavigationControllerPresenter : RNNBasePresenter +@property (nonatomic, strong) InteractivePopGestureDelegate *interactivePopGestureDelegate; + - (instancetype)initWithcomponentRegistry:(RNNReactComponentRegistry *)componentRegistry; - (void)applyOptionsBeforePopping:(RNNNavigationOptions *)options; diff --git a/lib/ios/RNNNavigationControllerPresenter.m b/lib/ios/RNNNavigationControllerPresenter.m index 1323026c91e..c7326e44e2a 100644 --- a/lib/ios/RNNNavigationControllerPresenter.m +++ b/lib/ios/RNNNavigationControllerPresenter.m @@ -28,6 +28,11 @@ - (void)applyOptions:(RNNNavigationOptions *)options { RNNNavigationController* navigationController = self.bindedViewController; + self.interactivePopGestureDelegate = [InteractivePopGestureDelegate new]; + self.interactivePopGestureDelegate.navigationController = navigationController; + self.interactivePopGestureDelegate.originalDelegate = navigationController.interactivePopGestureRecognizer.delegate; + navigationController.interactivePopGestureRecognizer.delegate = self.interactivePopGestureDelegate; + [navigationController rnn_setInteractivePopGestureEnabled:[options.popGesture getWithDefaultValue:YES]]; [navigationController rnn_setRootBackgroundImage:[options.rootBackgroundImage getWithDefaultValue:nil]]; [navigationController rnn_setNavigationBarTestID:[options.topBar.testID getWithDefaultValue:nil]]; diff --git a/lib/ios/ReactNativeNavigation.xcodeproj/project.pbxproj b/lib/ios/ReactNativeNavigation.xcodeproj/project.pbxproj index a7c1acb50f8..eb20d9107be 100644 --- a/lib/ios/ReactNativeNavigation.xcodeproj/project.pbxproj +++ b/lib/ios/ReactNativeNavigation.xcodeproj/project.pbxproj @@ -280,6 +280,8 @@ 7BEF0D1D1E43771B003E96B0 /* RNNLayoutNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BEF0D1B1E43771B003E96B0 /* RNNLayoutNode.m */; }; A7626BFD1FC2FB2C00492FB8 /* RNNTopBarOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = A7626BFC1FC2FB2C00492FB8 /* RNNTopBarOptions.m */; }; A7626C011FC5796200492FB8 /* RNNBottomTabsOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = A7626C001FC5796200492FB8 /* RNNBottomTabsOptions.m */; }; + C2A57A1C21E815F80066711C /* InteractivePopGestureDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = C2A57A1A21E815F80066711C /* InteractivePopGestureDelegate.h */; }; + C2A57A1D21E815F80066711C /* InteractivePopGestureDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = C2A57A1B21E815F80066711C /* InteractivePopGestureDelegate.m */; }; E33AC20020B5BA0B0090DB8A /* RNNSplitViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E33AC1FF20B5BA0B0090DB8A /* RNNSplitViewController.m */; }; E33AC20520B5C3890090DB8A /* RNNStatusBarOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = E33AC20320B5C3890090DB8A /* RNNStatusBarOptions.m */; }; E33AC20820B5C4F90090DB8A /* RNNSplitViewOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = E33AC20720B5C4F90090DB8A /* RNNSplitViewOptions.m */; }; @@ -617,6 +619,8 @@ A7626BFE1FC2FB6700492FB8 /* RNNTopBarOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNTopBarOptions.h; sourceTree = ""; }; A7626BFF1FC578AB00492FB8 /* RNNBottomTabsOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNBottomTabsOptions.h; sourceTree = ""; }; A7626C001FC5796200492FB8 /* RNNBottomTabsOptions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNBottomTabsOptions.m; sourceTree = ""; }; + C2A57A1A21E815F80066711C /* InteractivePopGestureDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InteractivePopGestureDelegate.h; sourceTree = ""; }; + C2A57A1B21E815F80066711C /* InteractivePopGestureDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = InteractivePopGestureDelegate.m; sourceTree = ""; }; D8AFADBD1BEE6F3F00A4592D /* libReactNativeNavigation.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libReactNativeNavigation.a; sourceTree = BUILT_PRODUCTS_DIR; }; E33AC1FF20B5BA0B0090DB8A /* RNNSplitViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNSplitViewController.m; sourceTree = ""; }; E33AC20120B5BA550090DB8A /* RNNSplitViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNSplitViewController.h; sourceTree = ""; }; @@ -719,6 +723,8 @@ 5038A3BC216E1490009280BC /* RNNTabBarItemCreator.m */, 5053CE7D2175FB1900D0386B /* RNNDefaultOptionsHelper.h */, 5053CE7E2175FB1900D0386B /* RNNDefaultOptionsHelper.m */, + C2A57A1A21E815F80066711C /* InteractivePopGestureDelegate.h */, + C2A57A1B21E815F80066711C /* InteractivePopGestureDelegate.m */, ); name = Helpers; sourceTree = ""; @@ -1206,6 +1212,7 @@ E8AEDB4A1F5C0BAF000F5A6A /* RNNInteractivePopAnimator.h in Headers */, 263905B71E4C6F440023D7D3 /* UIViewController+MMDrawerController.h in Headers */, 263905BB1E4C6F440023D7D3 /* RCCDrawerHelper.h in Headers */, + C2A57A1C21E815F80066711C /* InteractivePopGestureDelegate.h in Headers */, 263905CC1E4C6F440023D7D3 /* SidebarWunderlistAnimation.h in Headers */, 507F43F41FF4FCFE00D9425B /* HMSegmentedControl.h in Headers */, 501CD31F214A5B6900A6E225 /* RNNLayoutInfo.h in Headers */, @@ -1515,6 +1522,7 @@ 50F5DFC21F407A8C001A00BC /* RNNTabBarController.m in Sources */, 50887CA920F26BFE00D06111 /* RNNOverlayWindow.m in Sources */, 5038A3CF216E35E0009280BC /* Dictionary.m in Sources */, + C2A57A1D21E815F80066711C /* InteractivePopGestureDelegate.m in Sources */, 263905BC1E4C6F440023D7D3 /* RCCDrawerHelper.m in Sources */, 4534E72620CB6724009F8185 /* RNNLargeTitleOptions.m in Sources */, 507E7D58201DDD3000444E6C /* RNNAnimationOptions.m in Sources */,