Skip to content

Commit 53ab8c8

Browse files
authored
iOS: Set shadow node size depending on interface orientation (#34)
* iOS: Set shadow node size depending on interface orientation * fix formatting
1 parent c037b82 commit 53ab8c8

File tree

7 files changed

+91
-28
lines changed

7 files changed

+91
-28
lines changed

common/cpp/react/renderer/components/multiplemodals/RNTModalViewUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
namespace facebook::react
66
{
7-
Size RNTModalViewSize(void);
7+
Size RNTModalViewSize(void);
88
} // namespace facebook::react

common/cpp/react/renderer/components/multiplemodals/RNTModalViewUtils.mm

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@
33
#import <Foundation/Foundation.h>
44
#import <React/RCTUtils.h>
55

6+
#import "RNTModalWindowHelper.h"
7+
68
namespace facebook::react {
79

810
Size RNTModalViewSize(void)
911
{
10-
CGSize screenSize = RCTScreenSize();
11-
return {screenSize.width, screenSize.height};
12+
__block CGSize screenSize = RCTScreenSize();
13+
14+
dispatch_sync(dispatch_get_main_queue(), ^{
15+
RNTModalWindowHelper *windowHelper = [[RNTModalWindowHelper alloc] init];
16+
UIInterfaceOrientation orientation = [windowHelper getWindowOrientation];
17+
18+
if (UIInterfaceOrientationIsLandscape(orientation)) {
19+
screenSize = CGSizeMake(screenSize.height, screenSize.width);
20+
}
21+
});
22+
23+
return { screenSize.width, screenSize.height };
1224
}
1325

14-
} // namespace facebook::react
26+
} // namespace facebook::react

ios/Library/RNTModalMountingHelper/RNTModalMountingHelper.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@
55
#import "RNTModalViewController.h"
66

77
@protocol RNTModalMountingHelperProtocol <NSObject>
8-
- (instancetype _Nonnull)initWithViewController:(RNTModalViewController *_Nonnull)viewController;
8+
99
- (void)updatePropsTransaction:(void (^__nullable)(void))completion;
1010
- (void)updateChildrenTransaction:(void (^__nullable)(void))completion;
1111
- (void)mountIfNeeded;
1212
- (void)unmountIfNeeded;
13+
1314
@end
1415

1516
@interface RNTModalMountingHelper : NSObject <RNTModalMountingHelperProtocol>
17+
- (instancetype _Nonnull)init NS_UNAVAILABLE;
18+
19+
- (instancetype _Nonnull)initWithViewController:(RNTModalViewController *_Nonnull)viewController NS_DESIGNATED_INITIALIZER;
1620

1721
@property(nonatomic, assign) BOOL isMounted;
1822
@property(nonatomic, assign) BOOL hasProps;
1923
@property(nonatomic, assign) BOOL hasChildren;
2024
@property(nullable, weak) RNTModalViewController *modal;
2125

22-
- (UIWindow *_Nullable)getKeyWindow;
23-
- (UIViewController *_Nullable)getRootController;
2426
- (void)mount;
2527
- (void)unmount;
2628

ios/Library/RNTModalMountingHelper/RNTModalMountingHelper.m

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import <UIKit/UIKit.h>
22
#import "RNTModalMountingHelper.h"
3+
#import "RNTModalWindowHelper.h"
34

45
@implementation RNTModalMountingHelper
56

@@ -17,25 +18,6 @@ - (instancetype _Nonnull)initWithViewController:(RNTModalViewController * _Nonnu
1718
return self;
1819
}
1920

20-
- (UIWindow *)getKeyWindow {
21-
for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
22-
if ([scene isKindOfClass:[UIWindowScene class]]) {
23-
UIWindowScene *windowScene = (UIWindowScene *)scene;
24-
for (UIWindow *window in windowScene.windows) {
25-
if (window.isKeyWindow) {
26-
return window;
27-
}
28-
}
29-
}
30-
}
31-
32-
return nil;
33-
}
34-
35-
- (UIViewController *)getRootController {
36-
return [self getKeyWindow].rootViewController;
37-
}
38-
3921
- (void)updateChildrenTransaction:(void (^ _Nullable)(void))completion {
4022
dispatch_async(dispatch_get_main_queue(), ^{
4123
if (completion) {
@@ -70,7 +52,8 @@ - (void)unmountIfNeeded {
7052
}
7153

7254
- (void)mount {
73-
UIViewController *rvc = [self getRootController];
55+
RNTModalWindowHelper *windowHelper = [[RNTModalWindowHelper alloc] init];
56+
UIViewController *rvc = [windowHelper getRootController];
7457

7558
if (!rvc) {
7659
NSLog(@"reactViewController not found");
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#import <UIKit/UIKit.h>
2+
#import "RNTModalWindowHelper.h"
3+
4+
@implementation RNTModalWindowHelper
5+
6+
- (UIWindow *)getKeyWindow
7+
{
8+
for (UIScene *scene in [UIApplication sharedApplication].connectedScenes)
9+
{
10+
if ([scene isKindOfClass:[UIWindowScene class]])
11+
{
12+
UIWindowScene *windowScene = (UIWindowScene *)scene;
13+
for (UIWindow *window in windowScene.windows)
14+
{
15+
if (window.isKeyWindow)
16+
{
17+
return window;
18+
}
19+
}
20+
}
21+
}
22+
23+
return nil;
24+
}
25+
26+
- (UIViewController *)getRootController
27+
{
28+
return [self getKeyWindow].rootViewController;
29+
}
30+
31+
- (UIInterfaceOrientation)getWindowOrientation
32+
{
33+
return [self getKeyWindow].windowScene.interfaceOrientation ?: UIInterfaceOrientationPortrait;
34+
}
35+
36+
@end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef RNTModalWindowHelper_h
2+
#define RNTModalWindowHelper_h
3+
4+
#import <UIKit/UIKit.h>
5+
6+
@protocol RNTModalWindowHelperProtocol <NSObject>
7+
8+
- (UIWindow *_Nullable)getKeyWindow;
9+
- (UIViewController *_Nullable)getRootController;
10+
- (UIInterfaceOrientation)getWindowOrientation;
11+
12+
@end
13+
14+
@interface RNTModalWindowHelper : NSObject <RNTModalWindowHelperProtocol>
15+
16+
@end
17+
18+
#endif /* RNTModalWindowHelper_h */

ios/RNTModalShadowView.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,26 @@
33
#import "RCTBridge.h"
44
#import "RCTShadowView.h"
55
#import "RCTUtils.h"
6+
#import "RNTModalWindowHelper.h"
67

78
@implementation RNTModalShadowView
89

910
- (void)insertReactSubview:(id<RCTComponent>)subview atIndex:(NSInteger)atIndex
1011
{
1112
[super insertReactSubview:subview atIndex:atIndex];
1213
if ([subview isKindOfClass:[RCTShadowView class]]) {
13-
((RCTShadowView *)subview).size = RCTScreenSize();
14+
dispatch_sync(dispatch_get_main_queue(), ^{
15+
RNTModalWindowHelper *windowHelper = [[RNTModalWindowHelper alloc] init];
16+
UIInterfaceOrientation orientation = [windowHelper getWindowOrientation];
17+
18+
CGSize screenSize = RCTScreenSize();
19+
20+
if (UIInterfaceOrientationIsPortrait(orientation)) {
21+
((RCTShadowView *)subview).size = screenSize;
22+
} else {
23+
((RCTShadowView *)subview).size = CGSizeMake(screenSize.height, screenSize.width);
24+
}
25+
});
1426
}
1527
}
1628

0 commit comments

Comments
 (0)