18

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)?

Patrick
  • 1,410
  • 2
  • 19
  • 37

3 Answers3

1

You can use container view controller to kind of compose 2 view controllers into one.

  1. Create containerViewController:

    UIViewController* containerViewController = [[UIViewController alloc] init];
    
  2. Create your child view controllers, that you will add to container view controller:

    In your case they will be rootViewController and adViewController, in the example I'll call them firstChildViewController and secondChildViewController).

    UIViewController* firstChildViewController = [[UIViewController alloc] init];
    UIViewController* secondChildViewController = [[UIViewController alloc] init];
    
  3. Add your child view controllers to container view controller in right order:

    If firstChildViewController should be above secondChildViewController, then first add secondChildViewController and then firstChildViewController 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.

LowKostKustomz
  • 424
  • 4
  • 8
0

Try this it may helps, but probably solving this is React is better than changing screen size

UIViewController *rootViewController = [[UIViewController alloc] init];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
rootViewController.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-50);
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Clown
  • 163
  • 1
  • 12
0

As suggested by mihir mehta storyboard is most convenient way to handle UI in iOS. Here is link from where you can learn to add story board. Creating a new storyboard in XCode and making it the main one (black screen in simulator) Also there are ample tutorials on subject.

Stakshi
  • 389
  • 1
  • 15