What you can actually do is have the UIViewController added as a child to the key window view controller but still have the view of the UIViewController added as a subview to the native module view so it's contained in the frame and lifecycle of the native module view without presenting the UIViewController itself outside the life of the native module view as so ->
UIWindow *window = (UIWindow*)[[UIApplication sharedApplication] keyWindow];
[window.rootViewController addChildViewController:_myViewController];
_myViewController.view.frame = self.superview.frame;
[self addSubview:_myViewController.view];
[_myViewController didMoveToParentViewController:window.rootViewController];
Here is the full implementation ->
In your MyViewManager.m add
#import "RCTUIManager.h"
@interface MyViewManager ()
@end
@implementation MyViewManager
RCT_EXPORT_MODULE()
- (UIView*)view
{
return [[MyView alloc] init];
}
@end
and in your MyView.h add
#if __has_include("React/RCTViewManager.h")
#import "React/RCTViewManager.h"
#else
#import "RCTViewManager.h"
#endif
#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>
@interface MyView : UIView
@property(nonatomic, strong)UIViewController* myViewController;
@end
and in your MyView.m add
#import "MyView.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
@implementation MyView
- (instancetype)init
{
self = [super init];
return self;
}
- (void)dealloc
{
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (_myViewController != nil) {
_myViewController.view.frame = self.frame;
}
}
- (void)removeFromSuperview {
if (_myViewController != nil) {
[_myViewController willMoveToParentViewController:nil];
[_myViewController.view removeFromSuperview];
[_myViewController removeFromParentViewController];
_myViewController = nil;
[super removeFromSuperview];
}
}
-(void)addViewControllerAsSubView
{
_myViewController = [UIViewController new];
UIWindow *window = (UIWindow*)[[UIApplication sharedApplication] keyWindow];
[window.rootViewController addChildViewController:_myViewController];
_myViewController.view.frame = self.superview.frame;
[self addSubview:_myViewController.view];
[_myViewController didMoveToParentViewController:window.rootViewController];
}