I am trying to add an ad unit above a subclass of UIView RCTRootView
. This would move the RCTRootView
down about 50px. This is my current attempt.
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"test"
launchOptions:launchOptions];
CGRect frame = rootView.frame;
frame.size.height -= 50;
rootView.frame = frame;
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.autoresizesSubviews = YES;
UIViewController *rootViewController = [[UIViewController alloc] init];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
This is a React Native project, so use of a StoryBoard. How can I resize the rootView?
How can I instantiate and append a viewController within UIWindow that is above the rootView (Ad unit)?
You can use container view controller to kind of compose 2 view controllers into one.
Create containerViewController:
UIViewController* containerViewController = [[UIViewController alloc] init];
Create your child view controllers, that you will add to container view controller:
In your case they will be
rootViewController
andadViewController
, in the example I'll call themfirstChildViewController
andsecondChildViewController
).
UIViewController* firstChildViewController = [[UIViewController alloc] init];
UIViewController* secondChildViewController = [[UIViewController alloc] init];
Add your child view controllers to container view controller in right order:
If
firstChildViewController
should be abovesecondChildViewController
, then first addsecondChildViewController
and thenfirstChildViewController
to create right subviews hierarchy).Do not forget about layout your child view controller's views (TODO's are set where it is needed).
[containerViewController addChildViewController:firstChildViewController];
[containerViewController.view addSubview:firstChildViewController.view];
// TODO: layout firstChildViewController.view in containerViewController.view here
[firstChildViewController didMoveToParentViewController:containerViewController];
[containerViewController addChildViewController:secondChildViewController];
[containerViewController.view addSubview:secondChildViewController.view];
// TODO: layout secondChildViewController.view in containerViewController.view here
[secondChildViewController didMoveToParentViewController:containerViewController];
To layout container view controller's views trust them as regular
UIView
's and use preferred method to set their coordinates and sizes (autolayout or manual layout).To move view down 50px you should, for example, constraint bottom of the top view to top of the bottom view and height of the top view to 50.
There you go, you have two view controllers layed out as you need and all you need to do is to set containerViewController
to window
's rootViewController
:
[window setRootViewController:containerViewController];
Hope it will help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With