Skip to content

Instantly share code, notes, and snippets.

@MegaMaddin
Created May 13, 2021 14:16
Show Gist options
  • Save MegaMaddin/a711a9de32bae4b3c296e1bdb548c722 to your computer and use it in GitHub Desktop.
Save MegaMaddin/a711a9de32bae4b3c296e1bdb548c722 to your computer and use it in GitHub Desktop.
[react-native][iOS] Trigger onRequestClose when swiping down a pageSheet
diff --git a/node_modules/react-native/React/Views/RCTModalHostView.h b/node_modules/react-native/React/Views/RCTModalHostView.h
index c54c1c6..a5c49d1 100644
--- a/node_modules/react-native/React/Views/RCTModalHostView.h
+++ b/node_modules/react-native/React/Views/RCTModalHostView.h
@@ -16,7 +16,7 @@
@protocol RCTModalHostViewInteractor;
-@interface RCTModalHostView : UIView <RCTInvalidating>
+@interface RCTModalHostView : UIView <RCTInvalidating, UIAdaptivePresentationControllerDelegate>
@property (nonatomic, copy) NSString *animationType;
@property (nonatomic, assign) UIModalPresentationStyle presentationStyle;
@@ -30,6 +30,7 @@
@property (nonatomic, copy) NSArray<NSString *> *supportedOrientations;
@property (nonatomic, copy) RCTDirectEventBlock onOrientationChange;
+@property (nonatomic, copy) RCTDirectEventBlock onRequestClose;
- (instancetype)initWithBridge:(RCTBridge *)bridge NS_DESIGNATED_INITIALIZER;
diff --git a/node_modules/react-native/React/Views/RCTModalHostView.m b/node_modules/react-native/React/Views/RCTModalHostView.m
index 0cfa69a..16a5f1c 100644
--- a/node_modules/react-native/React/Views/RCTModalHostView.m
+++ b/node_modules/react-native/React/Views/RCTModalHostView.m
@@ -35,6 +35,10 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
_bridge = bridge;
_modalViewController = [RCTModalHostViewController new];
UIView *containerView = [UIView new];
+ // Transparency breaks for overFullScreen in iOS < 13
+ if (@available(iOS 13.0, *)) {
+ _modalViewController.presentationController.delegate = self;
+ }
containerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_modalViewController.view = containerView;
_touchHandler = [[RCTTouchHandler alloc] initWithBridge:bridge];
@@ -49,6 +53,11 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
return self;
}
+- (void)setOnRequestClose:(RCTDirectEventBlock)onRequestClose
+{
+ _onRequestClose = onRequestClose;
+}
+
- (void)notifyForBoundsChange:(CGRect)newBounds
{
if (_reactSubview && _isPresented) {
@@ -200,4 +209,30 @@ - (UIInterfaceOrientationMask)supportedOrientationsMask
return supportedOrientations;
}
+// MARK: - UIAdaptivePresentationControllerDelegate -
+// Method must be implemented, otherwise iOS defaults to 'automatic' (pageSheet on >= iOS 13.0)
+- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection
+{
+ if (self.presentationStyle == UIModalPresentationFullScreen && self.isTransparent) {
+ return UIModalPresentationOverFullScreen;
+ }
+ return self.presentationStyle;
+}
+
+// Method must be implemented, otherwise iOS defaults to 'automatic' (pageSheet on >= iOS 13.0)
+- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
+{
+ if (self.presentationStyle == UIModalPresentationFullScreen && self.isTransparent) {
+ return UIModalPresentationOverFullScreen;
+ }
+ return self.presentationStyle;
+}
+
+- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController
+{
+ if (_onRequestClose) {
+ _onRequestClose(nil);
+ }
+}
+
@end
diff --git a/node_modules/react-native/React/Views/RCTModalHostViewController.m b/node_modules/react-native/React/Views/RCTModalHostViewController.m
index 00c149d..16dbfcc 100644
--- a/node_modules/react-native/React/Views/RCTModalHostViewController.m
+++ b/node_modules/react-native/React/Views/RCTModalHostViewController.m
@@ -22,12 +22,12 @@ - (instancetype)init
return nil;
}
-#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
- __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
- if (@available(iOS 13.0, *)) {
- self.modalInPresentation = YES;
- }
-#endif
+//#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
+// __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
+// if (@available(iOS 13.0, *)) {
+// self.modalInPresentation = YES;
+// }
+//#endif
_preferredStatusBarStyle = [RCTSharedApplication() statusBarStyle];
_preferredStatusBarHidden = [RCTSharedApplication() isStatusBarHidden];
diff --git a/node_modules/react-native/React/Views/RCTModalHostViewManager.m b/node_modules/react-native/React/Views/RCTModalHostViewManager.m
index 91d83aa..d5ac1d7 100644
--- a/node_modules/react-native/React/Views/RCTModalHostViewManager.m
+++ b/node_modules/react-native/React/Views/RCTModalHostViewManager.m
@@ -120,5 +120,6 @@ - (void)invalidate
RCT_EXPORT_VIEW_PROPERTY(identifier, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(supportedOrientations, NSArray)
RCT_EXPORT_VIEW_PROPERTY(onOrientationChange, RCTDirectEventBlock)
+RCT_EXPORT_VIEW_PROPERTY(onRequestClose, RCTDirectEventBlock)
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment