Question

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. Then in MainTabController's viewDidLoad I move the tabBar up and add the red subview. 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.

what it should look like

Was it helpful?

Solution

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. 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".

enter image description here

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). 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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top