diff --git a/packages/react-native-carplay/ios/RNCarPlay.m b/packages/react-native-carplay/ios/RNCarPlay.m index 8bbc8b7c..a4f337a3 100644 --- a/packages/react-native-carplay/ios/RNCarPlay.m +++ b/packages/react-native-carplay/ios/RNCarPlay.m @@ -1,4 +1,5 @@ #import "RNCarPlay.h" +#import "RNCarPlayViewController.h" #import #import @@ -44,7 +45,7 @@ + (void) disconnect { RNCarPlay *cp = [RNCarPlay allocWithZone:nil]; RNCPStore *store = [RNCPStore sharedManager]; [store setConnected:false]; - [[store.window subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; + store.window.rootViewController = nil; if (cp.bridge) { [cp sendEventWithName:@"didDisconnect" body:@{}]; @@ -887,9 +888,8 @@ - (void) applyConfigForMapTemplate:(CPMapTemplate*)mapTemplate templateId:(NSStr if ([config objectForKey:@"render"]) { RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:self.bridge moduleName:templateId initialProperties:@{}]; - [rootView setFrame:store.window.frame]; - [[store.window subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; - [store.window addSubview:rootView]; + RNCarPlayViewController *viewController = [[RNCarPlayViewController alloc] initWithRootView:rootView]; + store.window.rootViewController = viewController; } } diff --git a/packages/react-native-carplay/ios/RNCarPlayViewController.h b/packages/react-native-carplay/ios/RNCarPlayViewController.h new file mode 100644 index 00000000..a9749b78 --- /dev/null +++ b/packages/react-native-carplay/ios/RNCarPlayViewController.h @@ -0,0 +1,10 @@ +// +// RNCarPlayViewController.h +// Created by Susan Thapa on 27/02/2024. +// +#import +#import + +@interface RNCarPlayViewController : UIViewController +- (instancetype)initWithRootView:(RCTRootView *)rootView; +@end diff --git a/packages/react-native-carplay/ios/RNCarPlayViewController.m b/packages/react-native-carplay/ios/RNCarPlayViewController.m new file mode 100644 index 00000000..5ba2f420 --- /dev/null +++ b/packages/react-native-carplay/ios/RNCarPlayViewController.m @@ -0,0 +1,50 @@ +// +// RNCarPlayViewController.m +// react-native-carplay +// +// Created by Susan Thapa on 27/02/2024. +// + +#import +#import "RNCarPlayViewController.h" +#import + +@interface RNCarPlayViewController () + +@property (nonatomic, strong) RCTRootView *rootView; + +@end + +@implementation RNCarPlayViewController + +- (instancetype)initWithRootView:(RCTRootView *)rootView { + self = [super init]; + if (self) { + _rootView = rootView; + } + return self; +} + +- (void)viewDidLoad { + [super viewDidLoad]; + self.view.translatesAutoresizingMaskIntoConstraints = false; + if (self.rootView) { + self.rootView.translatesAutoresizingMaskIntoConstraints = false; + self.rootView.frame = self.view.bounds; + [self.view addSubview:self.rootView]; + + [NSLayoutConstraint activateConstraints:@[ + [self.rootView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], + [self.rootView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], + [self.rootView.topAnchor constraintEqualToAnchor:self.view.topAnchor], + [self.rootView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], + ]]; + } +} + +- (void)viewWillLayoutSubviews { + [super viewWillLayoutSubviews]; + self.rootView.frame = self.view.bounds; +} + +@end diff --git a/packages/react-native-carplay/src/CarPlay.ts b/packages/react-native-carplay/src/CarPlay.ts index c4f629de..db2f5959 100644 --- a/packages/react-native-carplay/src/CarPlay.ts +++ b/packages/react-native-carplay/src/CarPlay.ts @@ -1,4 +1,10 @@ -import { ImageSourcePropType, NativeEventEmitter, NativeModule, NativeModules, Platform } from 'react-native'; +import { + ImageSourcePropType, + NativeEventEmitter, + NativeModule, + NativeModules, + Platform, +} from 'react-native'; import { ActionSheetTemplate } from './templates/ActionSheetTemplate'; import { AlertTemplate } from './templates/AlertTemplate'; import { ContactTemplate } from './templates/ContactTemplate';