0

If I create a basic ViewController and add an opaque UIToolbar to the view, like so:

UIViewController* viewController = [[UIViewController alloc] init];
[viewController.view setBackgroundColor:[UIColor blueColor]]; // To make it easy to see
self.window.rootViewController = viewController;

UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, viewController.view.frame.size.height - 49, viewController.view.frame.size.width, 49)]; // At bottom. Height of 49
[toolbar setBarStyle:UIBarStyleBlack]; // Opaque and easy to see

[self.window.rootViewController.view addSubview:toolbar]; // Add toolbar to view controller

The view hierarchy ends up like below:

View Controller behind Toolbar

How do I make the View controller resize it's main area (the blue part) so it doesn't extend behind the UIToolbar?

Vinodh
  • 5,262
  • 4
  • 38
  • 68
ABeard89
  • 911
  • 9
  • 17
  • just leave the viewController.view as is without setting color, add another UIView for blue part – Ravi Nov 21 '16 at 09:37
  • Would this be the same as adding the view from another ViewController as a subview? – ABeard89 Nov 21 '16 at 09:42
  • didn't get you @ ABeard – Ravi Nov 21 '16 at 09:44
  • If I had a second View Controller called `secondVC`, would this be what you're suggesting? `[viewController.view addSubview:secondVC.view];` – ABeard89 Nov 21 '16 at 09:49
  • in that case you can use containers, have a look at http://stackoverflow.com/questions/16884879/how-to-use-a-container-view-in-ios – Ravi Nov 21 '16 at 09:51
  • I've tried adding my secondary ViewController as a child view of the main VC. But that didn't have any effect. `[viewController addChildViewController:secondVC]; [secondVC didMoveToParentViewController:viewController];` Unless I'm doing it wrong. This guide and most are for using IB. – ABeard89 Nov 21 '16 at 09:59
  • http://stackoverflow.com/questions/27276561/adding-a-view-controller-as-a-subview-in-another-view-controller, verify your code with the accepted answer – Ravi Nov 21 '16 at 10:02
  • My previous comment is mostly doing what they said. The only difference is they set the frame for the child VC. If I set it to the frame of the parent VC, then it still extends behind the toolbar. If I don't set the frame, then it doesn't show up at all. If I have to calculate and set the frame, then it is not automatic. – ABeard89 Nov 21 '16 at 10:14

2 Answers2

0

Try to add this line of code

viewController.edgesForExtendedLayout = UIRectEdgeNone;

this will make so view controller's edges are not extended in any direction.

Or you can add this if you want to extend top, left and right, but not bottom.

viewController.edgesForExtendedLayout =  UIRectEdgeTop, UIRectEdgeLeft, UIRectEdgeRight;
Robert Khaireev
  • 739
  • 1
  • 7
  • 13
0

Try like this

UIViewController* viewController = [[UIViewController alloc] init];
    [viewController.view setBackgroundColor:[UIColor blueColor]]; // To make it easy to see
    self.window.rootViewController = viewController;

UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, viewController.view.frame.size.height - 49, viewController.view.frame.size.width, 49)]; // At bottom. Height of 49
[toolbar setBarStyle:UIBarStyleBlack]; // Opaque and easy to see

SecondViewController* secondChildVC = [self.storyboard instantiateViewControllerWithIdentifier:@"someId"];
[secondChildVC.view setBackgroundColor:[UIColor redColor]];
[viewController addChildViewController:secondChildVC];
[secondChildVC didMoveToParentViewController:viewController];
secondChildVC.view.frame = CGRectMake(0,0,viewController.view.frame.size.width,viewController.view.frame.size.height - 49);
[viewController.view addSubview:secondChildVC.view];

[self.window.rootViewController.view addSubview:toolbar];
Ravi
  • 2,441
  • 1
  • 11
  • 30
  • Do I have to calculate the frame of the second VC? `secondChildVC.view.frame = CGRectMake(0,0,viewController.view.frame.size.width,viewController.view.frame.size.height - 49);` This kind of calculation is exactly what I don't want to do. – ABeard89 Nov 21 '16 at 10:36
  • I want the main VC to automatically calculate the correct size for the second VC. Is there no way to do this? – ABeard89 Nov 21 '16 at 10:37
  • In other words, I need the main VC to act like a UITabBarController or a UINavigationController. Both of those automatically resize the content area without needing to calculate it myself. – ABeard89 Nov 21 '16 at 10:38
  • if you don't want to calculate frame, then you can use NSLayoutConstraints – Ravi Nov 21 '16 at 10:50
  • I'm starting to think that, myself. I'm trying to follow this guide here: http://stackoverflow.com/questions/31651022/how-to-create-layout-constraints-programmatically – ABeard89 Nov 21 '16 at 10:53
  • To add constraints this way, will I need to include the second VC? – ABeard89 Nov 21 '16 at 10:55
  • but i think its better to do with storyboard and its very easy to set constraints using storyboard – Ravi Nov 21 '16 at 10:56
  • I'd love to do it with storyboard. But I can't on this project. :( – ABeard89 Nov 21 '16 at 11:05
  • Thanks for your help, by the way. – ABeard89 Nov 21 '16 at 11:05