How to Add Navigation Interface After Create a Tab Bar Controller Programmatically (Swift)

How to add navigation interface after create a tab bar controller programmatically (Swift)

To create a navigation controller use:

var navigationController = UINavigationController(rootViewController: viewController));

Then just put it in the array:

tabBarController.viewControllers = [purchaseViewController, financeViewController, navigationController]

But, you can to add UIControls to navigation bar only after view controller did load. On applicationDidFinishLaunching: it's impossible, because the navigationBar is nil. Proper way is to add controls to navigationBar in the viewDidLoad()

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

Swift: How to transition from Navigation Controller to Tab Bar Controler

Okey I found the issue. Thanks @Paulw11 for pointing out that a Tab Bar Viewcontroller can be used just like a normal view controller. The problem was the part "
as? MainViewController", this had to say as? UITabBarController and voila, it worked. For anyone in the future, thats how you do it.

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
}

How add tabs programmatically in UITabBarController with swift?

UPDATE SWIFT 5

One example of how to create an UITabBarController programmatically could be like this:

First we create the UIViewControllers that will be the content for each tab of the tab bar interface. For this example we only create one very simple.

class Item1ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = UIColor.green
self.title = "item1"
print("item 1 loaded")
}
}

Now, the UITabBarController:

We create the new instances of the UIViewControllers that we want to display in the tab bar. Then we create an icon for each instance we have created and then we create an array that contains all UIViewControllers that specify the content for each tab of the tab bar interface. The order of the view controllers in the array corresponds to the display order in the tab bar.

class DashboardTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let item1 = Item1ViewController()
let icon1 = UITabBarItem(title: "Title", image: UIImage(named: "someImage.png"), selectedImage: UIImage(named: "otherImage.png"))
item1.tabBarItem = icon1
let controllers = [item1] //array of the root view controllers displayed by the tab bar interface
self.viewControllers = controllers
}

//Delegate methods
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
print("Should select viewController: \(viewController.title ?? "") ?")
return true;
}
}

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/

Swift: Navigation Bar disappears after programatically embedding tab bar controller

If your desired view controllers are embedded in UINavigationController instances you need to instantiate those rather than the desired view controllers directly. The storyboard will take care of instantiating the embedded view controllers.

So, if your two navigation controller scenes have "profileNavController" and "chatInboxNavController" as their identifiers, your code would be -

// Set up the tab and navigation bar controllers
var currController = window?.rootViewController

let chatSB = UIStoryboard(name: "Chat", bundle: nil)
let mainSB = UIStoryboard(name: "Main", bundle: nil)

let tabBarController = UITabBarController()
var navigationController = UINavigationController(rootViewController: currController!)

let profileNavController = mainSB.instantiateViewControllerWithIdentifier("profileNavController") as UINavigationController
let chatNavController = chatSB.instantiateViewControllerWithIdentifier("chatInboxNavController") as UINavigationController

tabBarController.viewControllers = [profileNavController, chatNavController, navigationController]
window?.rootViewController = tabBarController


Related Topics



Leave a reply



Submit