调整视图大小以适应工具栏

时间:2016-11-21 09:32:07

标签: ios uitoolbar

如果我创建一个基本的ViewController并向视图中添加一个不透明的UIToolbar,就像这样:

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

视图层次结构如下所示:

View Controller behind Toolbar

如何让View控制器调整其主区域(蓝色部分),使其不会延伸到UIToolbar后面?

2 个答案:

答案 0 :(得分:0)

尝试添加此行代码

viewController.edgesForExtendedLayout = UIRectEdgeNone;

这将使视图控制器的边缘不会向任何方向延伸。

或者,如果您想要扩展顶部,左侧和右侧,而不是底部,则可以添加此项。

viewController.edgesForExtendedLayout =  UIRectEdgeTop, UIRectEdgeLeft, UIRectEdgeRight;

答案 1 :(得分:0)

试试这个

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