Skip to content

Commit 333058e

Browse files
authored
iOS: Open modal in a new UIWindow (#37)
* iOS: Open modal in a new UIWindow * iOS: clear modal window on dismiss * iOS: hide UIWindow before removal
1 parent c481337 commit 333058e

File tree

5 files changed

+29
-14
lines changed

5 files changed

+29
-14
lines changed

ios/Library/RNTModalMountingHelper/RNTModalMountingHelper.m

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
#import "RNTModalMountingHelper.h"
33
#import "RNTModalWindowHelper.h"
44

5-
@implementation RNTModalMountingHelper
6-
7-
- (instancetype)init
8-
{
9-
[NSException raise:@"init" format:@"init has not been implemented"];
10-
return self;
5+
@implementation RNTModalMountingHelper {
6+
__strong UIWindow *modalWindow;
117
}
128

139
- (instancetype _Nonnull)initWithViewController:(RNTModalViewController * _Nonnull)viewController {
@@ -53,7 +49,8 @@ - (void)unmountIfNeeded {
5349

5450
- (void)mount {
5551
RNTModalWindowHelper *windowHelper = [[RNTModalWindowHelper alloc] init];
56-
UIViewController *rvc = [windowHelper getRootController];
52+
modalWindow = [windowHelper createNewKeyWindow];
53+
UIViewController *rvc = modalWindow.rootViewController;
5754

5855
if (!rvc) {
5956
NSLog(@"reactViewController not found");
@@ -69,10 +66,12 @@ - (void)mount {
6966

7067
- (void)unmount {
7168
[self.modal dismiss];
72-
_modal = nil;
73-
_isMounted = NO;
74-
_hasProps = NO;
75-
_hasChildren = NO;
69+
self.modal = nil;
70+
self.isMounted = NO;
71+
self.hasProps = NO;
72+
self.hasChildren = NO;
73+
modalWindow.hidden = YES;
74+
modalWindow = nil;
7675
}
7776

7877
@end

ios/Library/RNTModalViewController/RNTModalViewController.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ - (void)viewDidLayoutSubviews
5656
#pragma mark - ModalViewControllerProtocol
5757

5858
- (void)presentOn:(UIViewController *)parentVC onView:(UIView *)parentView {
59-
self.modalPresentationStyle = UIModalPresentationCustom;
60-
self.view.accessibilityViewIsModal = YES;
61-
6259
[self willMoveToParentViewController:parentVC];
6360
[parentVC addChildViewController:self];
6461
[parentView addSubview:self.view];

ios/Library/RNTModalWindowHelper/RNTModalWindowHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- (UIWindow *_Nullable)getKeyWindow;
99
- (UIViewController *_Nullable)getRootController;
1010
- (UIInterfaceOrientation)getWindowOrientation;
11+
- (UIWindow *_Nonnull)createNewKeyWindow;
1112

1213
@end
1314

ios/Library/RNTModalWindowHelper/RNTModalWindowHelper.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,20 @@ - (UIInterfaceOrientation)getWindowOrientation
3333
return [self getKeyWindow].windowScene.interfaceOrientation ?: UIInterfaceOrientationPortrait;
3434
}
3535

36+
- (UIWindow *)createNewKeyWindow {
37+
UIWindow *keyWindow = [self getKeyWindow];
38+
39+
UIWindow *nextWindow = [[UIWindow alloc] initWithFrame: keyWindow.bounds];
40+
41+
nextWindow.windowScene = keyWindow.windowScene;
42+
nextWindow.rootViewController = [[UIViewController alloc] init];
43+
nextWindow.backgroundColor = UIColor.clearColor;
44+
nextWindow.accessibilityViewIsModal = YES;
45+
nextWindow.rootViewController.modalPresentationStyle = UIModalPresentationCustom;
46+
47+
[nextWindow makeKeyAndVisible];
48+
49+
return nextWindow;
50+
}
51+
3652
@end

ios/RNTModalView.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ - (void)removeReactSubview:(UIView *)subview
4848
[self.mountingHelper unmountIfNeeded];
4949
[subview removeFromSuperview];
5050
self.reactSubview = nil;
51+
self.modalViewController = nil;
52+
self.mountingHelper = nil;
5153
});
5254
}
5355

0 commit comments

Comments
 (0)