From abc6e1cbf4cf9775205b0246404b0059a5838fba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Van=20der=20Auwermeulen?= Date: Thu, 15 Jun 2023 08:58:24 -0700 Subject: [PATCH] feat: allow custom assignment of rootView to rootViewController (#37873) Summary: To use a native Drawer on iPad, I can override `createRootViewController` to create a `UISplitViewController` instead of a `UIViewController`, but I then need to assign the rootView with ```objective-c [splitViewController setViewController:mainVC forColumn:UISplitViewControllerColumnSecondary]; ``` which I can currently only do by copy pasting the entire `didFinishLaunchingWithOptions` and only replacing the assignment ```objective-c rootViewController.view = rootView; ``` In an attempt of making it easier for developers to use a native drawer in iOS, being able to override the assignment would make it easier. bypass-github-export-checks ## Changelog: [iOS] [ADDED] - added override method with default implementation Pull Request resolved: https://github.com/facebook/react-native/pull/37873 Test Plan: Tested on iPad iOS 16 simulator Reviewed By: cortinico Differential Revision: D46761919 Pulled By: cipolleschi fbshipit-source-id: c3ece0170d732133edc08f220a2f9a67da93815a --- .../Libraries/AppDelegate/RCTAppDelegate.h | 11 +++++++++++ .../Libraries/AppDelegate/RCTAppDelegate.mm | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.h b/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.h index f2df4437c3da92..52763c21c4b7e9 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.h +++ b/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.h @@ -36,6 +36,7 @@ * - (UIView *)createRootViewWithBridge:(RCTBridge *)bridge moduleName:(NSString*)moduleName initProps:(NSDictionary *)initProps; * - (UIViewController *)createRootViewController; + * - (void)setRootView:(UIView *)rootView toRootViewController:(UIViewController *)rootViewController; * New Architecture: * - (BOOL)concurrentRootEnabled * - (BOOL)turboModuleEnabled; @@ -94,6 +95,16 @@ */ - (UIViewController *)createRootViewController; +/** + * It assigns the rootView to the rootViewController + * By default, it assigns the rootView to the view property of the rootViewController + * If you are not using a simple UIViewController, then there could be other methods to use to setup the rootView. + * For example: UISplitViewController requires `setViewController(_:for:)` + * + * @return: void + */ +- (void)setRootView:(UIView *)rootView toRootViewController:(UIViewController *)rootViewController; + /// This method controls whether the App will use RuntimeScheduler. Only applicable in the legacy architecture. /// /// @return: `YES` to use RuntimeScheduler, `NO` to use JavaScript scheduler. The default value is `YES`. diff --git a/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm b/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm index 808c315c2f1bf4..ecc438ca4a4f7a 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm +++ b/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm @@ -77,7 +77,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIViewController *rootViewController = [self createRootViewController]; - rootViewController.view = rootView; + [self setRootView:rootView toRootViewController:rootViewController]; self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible]; @@ -129,6 +129,11 @@ - (UIViewController *)createRootViewController return [UIViewController new]; } +- (void)setRootView:(UIView *)rootView toRootViewController:(UIViewController *)rootViewController +{ + rootViewController.view = rootView; +} + - (BOOL)runtimeSchedulerEnabled { return YES;