简体   繁体   English

在故事板中的tabbar下添加一个视图

[英]add a view under tabbar in storyboard

I'm wondering if there's a way (preferably an easy one) to add a view under the tab bar, like shown on the attached picture, so that it stays there when I switch between tabs and when new view controllers are pushed/popped on each tab. 我想知道是否有一种方法(最好是一个简单的方法)在标签栏下添加一个视图,如附图中所示,这样当我在标签之间切换和按下/弹出新的视图控制器时,它就会停留在那里每个标签。

I have subclassed UITabBarController as "MainTabController" and set is as the custom class for the Main Tab Bar Controller Scene in my storyboard. 我将UITabBarController子类化为“MainTabController”,并将其设置为我的故事板中主标签栏控制器场景的自定义类。 Then in MainTabController's viewDidLoad I move the tabBar up and add the red subview. 然后在MainTabController的viewDidLoad中,我移动tabBar并添加红色子视图。 So far so good, but the main view area size is still like it was before, with the tab bar at the bottom, so now the last few elements of the table stay below the tab bar. 到目前为止这么好,但主视图区域大小仍然像以前一样,底部的标签栏,所以现在表格的最后几个元素保持在标签栏下方。

I would very much like to avoid adjusting the frame size for each and every controller I push into either of the tabs, so I hope that there's a simpler solution that I'm not aware of. 我非常希望避免调整我推入任何一个选项卡的每个控制器的帧大小,所以我希望有一个我不知道的更简单的解决方案。

应该是什么样子

You can do this by embedding your tab bar controller in a custom container controller. 您可以通过将标签栏控制器嵌入自定义容器控制器中来完成此操作。 You can do this in a storyboard by making a UIViewController the initial view controller, and adding a container view to it. 您可以通过将UIViewController设置为初始视图控制器并向其添加容器视图来在故事板中执行此操作。 Size that container view so it has whatever space you need underneath, and add your view in that space. 调整容器视图的大小,使其在下方具有您需要的任何空间,并在该空间中添加视图。 Delete the view controller that comes automatically embedded in the container view, and control drag from the container view to your tab bar controller, and choose "embed". 删除自动嵌入容器视图中的视图控制器,并控制从容器视图拖动到选项卡栏控制器,然后选择“嵌入”。

在此输入图像描述

Be aware that there's a bug in the way tab bar controllers layout their views in iOS 7. If you put a view near the bottom of one of the controllers, it will appear the first time you see it, but move down under the tab bar when you select another tab and then come back again (it will reappear if you rotate to landscape and then back again). 请注意,标签栏控制器在iOS 7中布局视图的方式存在错误。如果您将视图放在其中一个控制器的底部附近,它将在您第一次看到它时出现,但在标签栏下向下移动当您选择另一个选项卡然后再次返回时(如果您旋转到横向然后再返回,它将重新出现)。 You can get around this bug by making your container view controller (the initial controller in my storyboard) the delegate of the tab bar controller, and adding this code. 您可以通过使容器视图控制器(我的故事板中的初始控制器)成为选项卡栏控制器的委托并添加此代码来解决此错误。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // called by the embed segue
    UITabBarController *tbc = segue.destinationViewController;
    tbc.delegate = self;
}


-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [tabBarController.view setNeedsLayout];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM