Resize a UIView to allow space for ad unit - ios

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 and adViewController, in the example I'll call them firstChildViewController and secondChildViewController).
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 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.

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

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.

Related

ViewController loaded in AppDelegate BUT shifted down

Inside AppDelegate.m file I have:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.MainVC = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.HamMenuVC = [[HamMenuViewController alloc] initWithNibName:#"HamMenuViewController" bundle:nil];
self.RevealVC = [[SWRevealViewController alloc]initWithRearViewController:self.HamMenuVC frontViewController:self.MainVC];
self.navC = [[UINavigationController alloc]initWithRootViewController:self.RevealVC];
self.window.rootViewController = self.navC;
[self.window makeKeyAndVisible];
Which is supposed to load my Main VC and then the hamburger menu VC (or slider? in iOS talk), and when I start the app I get the loaded VCs BUT the entire view seems to be shifted down roughly 30 pixels and there's this white space above.
As you can see below, this is the top of my screen and the black is where the MainVC starts:
the top of the screen
I've checked the 'self.view.frame.origin.y' inside the MainVC and it says 0.0 AND I've checked the '[[UIScreen mainScreen] bounds].size.height' and it's giving me the correct height for the device I'm running it on AND I checked the '[[UIScreen mainScreen] bounds].origin.y' and it says 0.0.
Does anyone have a clue why all the data is saying it's in the right place but the fact is it's in the wrong place on my physical device AND on any simulator I choose?
It looks like that's the UINavigationBar on top of the screen. In your code, you're setting your rootViewController with a UINavigationController.
self.navC = [[UINavigationController alloc]initWithRootViewController:self.RevealVC];
self.window.rootViewController = self.navC;
If you need to hide the navigation bar, call this method in your RevealVC's viewDidLoad method:
[self.navigationController setNavigationBarHidden:YES animated:YES];
Also, quick tip, try to name your properties starting with lowercase letters.

set current tableview inside new uiview

I've been making an App completely programmatically, but I would like to add some more buttons etc.
Currently my main view is a full-screen TableView.
I would like to load a UIView (from a NIB) which has some buttons / labels and my curreny TableView in the middle (full width) with sort of a header and footer with my buttons / labels.
(Since I suspect that changed need to be made here...) My AppDelegate currently has the following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
AFViewController *viewController = [[AFViewController alloc] initWithStyle:UITableViewStylePlain];
//AFViewController *viewController = [[AFViewController alloc] initWithNibName:#"mainView" bundle:nil];
self.viewController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
You need to create a UIViewController instead of UITableViewController. You can easily do in your current AFViewController which actually a UITableViewController, just follow these steps
Open AFViewController.h change UITableViewController to
UIViewController.
Then open AFViewController.xib and add a
UIView from controls.
Drag UITableView inside UIView.
Right click File Owner connect view to the UIView and create IBOutlet for the UITableView inside the UIView.
Thats it you are ready to go.. And at last..
AFViewController *viewController = [[AFViewController alloc] initWithNibName:#"AFViewController" bundle:nil];
Why don't you create the desired structure in one nib itself? Create a nib with view controller. On the nib place your buttons and tableview as per your layout.
Interface builder is intended to reduce your coding effort when it comes to design so use it. Make sure to make your new view controller as the root controller in app delegate.

Sidemenu with tabBarControllers in Storyboard

Hi Fellow iOS Developers, I am a newbie developing a project with 5 tab Views and on the first and second tabs I have slide out menus using Container views from example code by Michael Frederick on his GitHub page Project Link: https://github.com/mikefrederick/MFSideMenu. He is using a nib (.xib) files though I am using Storyboard to achieve the same and struck with defining the container and child views. can kindly some one advice how to modify the below code to accommodate in my storyboard.
the original code in the AppDelegate.m is
- (DemoViewController *)demoController {
return [[DemoViewController alloc] initWithNibName:#"DemoViewController" bundle:nil];
}
- (UINavigationController *)navigationController {
return [[UINavigationController alloc]
initWithRootViewController:[self demoController]];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:[self navigationController],
[self navigationController], nil]];
SideMenuViewController *leftSideMenuController = [[SideMenuViewController alloc] init];
SideMenuViewController *rightSideMenuController = [[SideMenuViewController alloc] init];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:tabBarController
leftMenuViewController:leftSideMenuController
rightMenuViewController:rightSideMenuController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
return YES;
}
#end
how to modify the code to accommodate the container parent view and child views ?
where should i instantiate the code for the parent and child of the 2nd tab view ? in AppDelegate or the View Controller ?
If any other Details are required leave a comment please. Any Help Will be greatly appreciated. thanks in Advance.
I don't know if you still need this, but i had the exactly same problem today, too. What you need to do is:
remove the both methods over your app Delegate
put this in your app Delegate:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"YOUR_STORYBOARD" bundle:[NSBundle mainBundle]];
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"THE_IDENTITY_OF_YOUR_SIDEMENU"];
UITabBarController *centerViewController = [storyboard instantiateViewControllerWithIdentifier:#"IDENTITY_OF_YOUR_TABBARCONTROLLER"];
[container setCenterViewController:centerViewController];
[container setLeftMenuViewController:leftSideMenuViewController]; //for the right Side, its the same way...
[container setPanMode:MFSideMenuPanModeNone]; //remove this line, if you need the pan mode
return YES;
In your Storyboard you have to put a ViewController as a subclass from "MFSideMenuContainerViewController". Mark this View as the "Initial View Controller" in the Attribute Inspector. Now use a Segue from your new Initial View Controller and let it "push" to your TabBarController. To avoid a Warning rename the Segue.
After you have done this, you can add a UIBarButtonItem to every View, you like to add the SideMenu. In the Action Method of this UIBarButtomItem you only need to do this:
[self.menuContainerViewController toggleLeftSideMenuCompletion:^{}];
finally make sure you have a UIViewController or a UITableViewController, that is your "SideMenu" and set the right Storyboard ID.
if you are still need help, comment this...
and sorry for my english :)
You can use https://github.com/ozcanakbulut/VoovilSideMenu. It's easy to embed in a tabBarController. It uses Storyboard and Arc.

Unable to add additional subview to uiwindow apart from rootviewcontroller

Below is my code from app delegate in didFinishLaunchingWithOptions
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
CGRect windowFrame = self.window.frame;
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, windowFrame.size.height-100, windowFrame.size.height, windowFrame.size.height)];
bottomView.backgroundColor = [UIColor redColor];
[self.window addSubview:bottomView];
// self.viewController.view.frame = CGRectMake(0, 0, windowFrame.size.width, windowFrame.size.height-100);
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
My ViewController is FreeForm and its height is just around 245. As you can see i am trying to put a bottom view that is 100 points height.
I am just trying to learn this.
But the view always filling the entire screen. If i comment out setting view controller as windows rootviewcontroller i can see my bottomview there on the screen.
What i am doing wrong? Please advice.
Thanks.
Instead of
self.window.rootViewController = self.viewController;
when i code
[self.window addSubview:self.viewController.view];
gave me the expected result and it governs the freeform size.
It appears when set as rootviewcontroller it fills the entire available screen regardless of the freeform size set on the XIB.
write only one sentance
[self.window bringSubviewToFront: bottomView];
OR
Add bottomView to window as ,
[[[[UIApplication sharedApplication] delegate] window] addSubview:bottomView];
I'm not sure why you're trying to do this, but the freeform setting in IB (at least for xib files) seems to be for layout purposes only. It doesn't actually affect the size you get when the view loads. If you want it to be shorter, then set its frame in the viewDidAppear: method in ViewController, not in the app delegate -- the view hasn't been loaded there yet, so it's too early to set the frame.

setRootViewController: doesn't work as expected after setViewControllers: have been called

I created a view controller programmatically and set it as a root controller. All worked perfect as it was expected:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CustomViewController *vc = [[CustomViewController alloc] init];
[[self window] setRootViewController:vc];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
Then I added an UITabBarViewController, set its 'viewControllers' property to point to (an array to) the main viewController 'vc'.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CustomViewController *vc = [[CustomViewController alloc] init];
UITabBarController *tbc = [[UITabBarController alloc] init];
NSArray *controllers = #[vc];
[tbc setViewControllers:controllers];
[[self window] setRootViewController:vc];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
and the view stoped to show at the screen upon launch, also there is warning about the root view controller wasn't set. After adding the string below the view of 'vc' controller is finally loaded, but there is a blank line at the bottom of the screen, as if the UIBarController modified [[UIMainScreen bounds].
[[self window] addSubview:vc.view];
I'm new to iOS development, and I'm a bit confused. It seems I don't understand some very fundamental things about the view controllers hierarchy, but after reading the "View Controller Programming Guide" by Apple, I still don't understand where am I wrong.
The setRootViewController: method should auto assign the _view of argument view controller as default view of the window, but it doesn't happen if the named view controller was already previously pointed by viewControllers property of UITabBarViewController. Though I checked the debugger and found that 'vc' object isn't changed after setViewControllers: method is called.
Could you please explain me what is going on or point me to a documentation I should read?
UPDATE: I'm not going to insert the 'vc' controller into the 'tbc' controller. What I'd like is to display the 'vc' view fullscreen, as it would normally displayed without the code about 'tbc'.
From my point of view, adding another view (tbc in my case) should NOT affect this behaviour.
Of course, that's pretty useless from practical point of view, but I'd like to know what's going on under the hood.
You should set tbc as rootViewController.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CustomViewController *vc = [[CustomViewController alloc] init];
UITabBarController *tbc = [[UITabBarController alloc] init];
NSArray *controllers = #[vc];
[tbc setViewControllers:controllers];
[[self window] setRootViewController:tbc];
[[self window] addSubview:tbc.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
When you add vc to the tab bar controller, it becomes a child of that controller. The root view controller of the window can't be a child, which is why you get that error. If you want vc to be full screen, then don't put it in the tab bar controller, and then at some point, you switch the window's root view controller to be the tab bar controller (if that's what you want).
You haven't said what you want to use vc for. A better way, depending on its use, might be to present it modally (so it takes the whole screen) from whichever controller is in the first tab of your tab bar controller. Do this from viewDidAppear, and it will be the firsts thing the user sees when the app starts up.

Resources