Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize a UIView to allow space for ad unit

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

like image 475
Patrick Avatar asked May 29 '15 06:05

Patrick


1 Answers

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.

like image 128
LowKostKustomz Avatar answered Oct 24 '22 07:10

LowKostKustomz