Create Tab Bar Controller and Navigation Controller

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

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

Navigation controller with tab bar controller?

Each UIViewController in UITabBarController could be embedded in an UINavigationController at your convenience, that way you'll be able to use all of the features that you need.

Basically, you need to select the tableViewController, click on Editor menu item, select Embed in and click on Navigation Controller, ta daa.

UINavigationController Example

You can show or hide Navigation Bar if you need it using Interface Builder or programmatically in your Detail viewController as follows:

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = true
// Do stuff
}

Swift-How do I add Tab Bar AND Navigation Bar to a single view controller?

In your Storyboard, you should drag out a Tab Bar Controller and use that as the initial view controller. Then, you should embed each of the view controllers attached to the Tab Bar Controller inside Navigation Controllers (Editor menu: Embed In > Navigation Controller). Afterward, your Storyboard should look something like this:

Sample Image

The tab bar controller holds a tab bar and will manage switching between the other views attached to it, while the navigation controllers will place Navigation Bars at the top of each tab and help you manage navigation within the tab.

How should a tab bar controller be integrated into a navigation controller workflow?

I checked your requirements.

Although not recommended by Apple Inc. for regular use cases,

https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/TabBarControllers.html#//apple_ref/doc/uid/TP40011313-CH3-SW2

But it can be easily achieved by some work around to achieve the suggested design principle through following steps.

  1. Create similar design in story board, do notice that Navigation Controller is before the Login screen.

Sample Image


  1. Implement following list methods to invoke specific tab through List View programatically.
 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"test" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%ld", (long)[indexPath row]];
return cell;
}
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITabBarController *tbc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainTabbedViewController"];
tbc.selectedIndex= (long)[indexPath row];
[self showViewController:tbc sender:self];
}

  1. In case the list size is dynamic, you can create tabbar items also programatically.

https://24hrstech.wordpress.com/2012/08/19/create-tabbarcontroller-programmatically/

How to present navigation controller with tab bar controller

If you want to show MainAreaVC with both NavigationController and TabBarcontroller then you need to present the UITabBarController instead of MainAreaVC. So in storyboard set the Storyboard Id for your TabBarController something like TabbarVC or what ever you want then use it wit instantiateViewController to get UITabBarController.

let tabbarVC = self.storyboard?.instantiateViewController(withIdentifier: "TabbarVC") as! UITabBarController
self.present(tabbarVC, animated: true, completion: nil)

Tab Bar not recognising ViewControllers when they are embedded in Navigation Controllers swift

That's because the presented view controller by UITabBarController is actually UINavigationController. So it is not matching the class when you check it with

if viewController is CreationViewController

You can test it by simply printing the view controller you get from 'func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)' or putting a break point to that function.



Related Topics



Leave a reply



Submit