Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement tab bar controller with navigation controller in right way

I am using Storyboard and Xcode 6. I have next controllers and scenes in my Storyboard:

UINavigationController that has HomeViewController as a root. HomeViewController has a button that Show (e.g. Push) UITabBarController. UITabBarController has 4 UIViewControllers.

But my problem that after I Show UITabBarController there are no Navigation Bars in 4 UIViewControllers. But I supposed that if I Show (e.g. Push) UITabBarController then it should has embedded navigation controller that is initial controller in storyboard. Am I right? And if so how can I setup then navigation bar in Storyboard, because there are now default bar event in pushed tab bar that I see on storyboard. I have selected UIViewController and set simulated metrics in identity inspector to Translucent Navigation bar for the Top property, but I supposed it should be automatically added to this controller and to the tab bar without additional steps.

Or should I add new navigation controller for each tab bar items that will have their root view controllers?

The main question why I don't see navigation bar in storyboard using show (e.g. Push). For example if I add navigation controller and then set as root - tab bar controller then Xcode automatically add top navigation bar, but if the queue has an extra step like in my case HomeViewController the top navigation bar never appear automatically.

like image 523
Matrosov Oleksandr Avatar asked Dec 11 '14 11:12

Matrosov Oleksandr


People also ask

How do I add a tab bar to my navigation controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

How does tab bar controller work?

When the user selects a specific tab, the tab bar controller displays the root view of the corresponding view controller, replacing any previous views. (User taps always display the root view of the tab, regardless of which tab was previously selected. This is true even if the tab was already selected.)

How do I add a Tabbar controller to my storyboard?

First, select the “Navigation Controller” in MainStoryboard. storyboard. Then select “Editor” in the menu and choose “Embed in”, followed by “Tab Bar Controller”.


1 Answers

Hi you need to embed each view controller that is within the tab bar in a navigation controller of its own. So the flow is like so (HomeVC is embedded in a NavController of it's own):

                                         / --> `NavController` --> `ViewController1`                                          | --> `NavController` --> `ViewController2` `HomeViewController`-->`TabBarController`|--> `NavController` --> `ViewController3`                                          \--> `NavController` --> `ViewController4` 
  1. Go to Editor --> Embed In --> Tab Bar Controller (or Navigation Controller)

How to embed correctly

To answer your questions:

Each tab of a tab bar controller interface is associated with a custom (different [sic]) view controller. When the user selects a specific tab, the tab bar controller displays the root view of the corresponding view controller, replacing any previous views.

So the Root View Controller of the tab must be adjoined to a Navigation Controller; a navigation view controller must be next inline in order for the View Controller to inherit a Navigation. A Tab Bar switches views to whatever is next inline.

This document will help outline more information about it. https://developer.apple.com/documentation/uikit/uitabbarcontroller

like image 69
App Dev Guy Avatar answered Oct 21 '22 15:10

App Dev Guy