当前位置: 首页 > 知识库问答 >
问题:

调整视图大小以考虑工具栏

戚建华
2023-03-14

如果创建基本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

视图层次结构的结果如下:

我如何使视图控制器调整它的主要区域(蓝色部分),使其不会延伸到UIToolbar后面?

共有2个答案

姬歌者
2023-03-14

像这样试试

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];
湛宏旷
2023-03-14

尝试添加这行代码

viewController.edgesForExtendedLayout = UIRectEdgeNone;

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

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

viewController.edgesForExtendedLayout =  UIRectEdgeTop, UIRectEdgeLeft, UIRectEdgeRight;
 类似资料:
  • 我想调整一个视图的大小,开始像200px宽100px高,然后“扩展”到填充半个屏幕时,它被点击。它背后的所有视图都应该保持在同一个位置。我只想扩展该视图,填满半个屏幕,重叠当前屏幕上的任何内容。 null

  • 我有一个尺寸为800x800的图像,其大小为170 kb。我想将此图像调整为600x600。调整大小后,我希望缩小图像大小。我该怎么做?

  • 我有3个子视图(、、

  • 问题内容: 下面的代码调整位图的大小并保持宽高比。我想知道是否有一种更有效的大小调整方法,因为我想到了我正在编写android API中已经可用的代码。 问题答案: 使用方法:)

  • 问题内容: 我在这里使用Go调整大小包:https://github.com/nfnt/resize 我正在从S3中提取图像,如下所示: // this gives me data []byte 之后,我需要调整图像大小: // problem is that the original_image has to be of type image.Image 将图像上传到我的S3存储桶 // pro

  • 问题内容: 我正在尝试从目录加载图像并将其作为图标放置在中。但是,当图像尺寸很大时,就不能完全放入标签中。我尝试调整图像的大小以适合标签,但无法正常工作。我可以知道我要去哪里了吗? 问题答案: BufferedImage img = ImageIO.read(…); Image scaled = img.getScaledInstance(500, 500, Image.SCALE_SMOOTH)