How to dynamically populate Tab Bar controller's tab bars.? (in swift -- iOS 9)

Viewed 2805

I am developing an iOS application where the entry point is a login Screen. Which then after the login, segue to a tab bar controller. Now I want to dynamically populate the number and contents of the tab bar items based on which level the user has been logged in.

Eg ->

Level 1 Login - Tab Bar Items

  • Option A
  • Option B
  • Option C
  • Option D

Level 2 Login - Tab Bar Items

  • Option C
  • Option D

How can I dynamically bind a tab bar controller to some data, to create this kind od views.?

2 Answers

Swift 4 - Xcode 9


You can dynamically change the array of UIViewControllers used by tabBarController at runtime by using:

self.viewControllers = arrayOfUIViewControllers

In your specific case, have 2 arrays of UIViewControllers available and when the tabBarController loads (presumably after login), present an array for level 1 or present another array for level 2 depending on the login data.


To address a more broad question, it might also be helpful to demonstrate another case where the user can generate and remove tabs dynamically at runtime.

  1. In the storyboard I have a Tab Bar Controller connected to a View Controller.
  2. View Controller has the Storyboard ID "NewTab" and is a subclass of "ViewController" it also has two UIButtons "Add" and "Remove" and the Tab Bar Item Title is set to "1"
  3. The tab names will be coming from Tabs.swift which is my placeholder for CoreData where tab data is stored.

    class Tabs: NSObject {
        let tabs: [String] = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
    }
    
  4. ViewController.swift does pretty much everything.

    import UIKit
    class ViewController: UIViewController {
        let tabs = Tabs().tabs
        var array: [UIViewController] = []
        @IBAction func makeNewTab(_ sender: Any) {
            let newViewController: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewTab") as UIViewController
            array = (self.tabBarController?.viewControllers)!
            if array.count < tabs.count {
                newViewController.title = tabs[array.count]
                array.append(newViewController)
                self.tabBarController?.setViewControllers(array, animated: true)
            }
        }
        @IBAction func removeTab(_ sender: Any) {
            array = (self.tabBarController?.viewControllers)!
            if array.count > 1 {
                let index = array.index(of: self)
                array.remove(at: index!)
                self.tabBarController?.setViewControllers(array, animated: true)
            }
        }
    }
    

    So here you instantiate the list of tab names and an empty array of ViewControlers. @IBAction makeNewTab is linked the Add button and @IBAction removeTab is linked to the storyboard Remove button. The buttons won't work if there are less than 2 tabs or if you've run out of tab names.

I've chosen to include this example in my answer because I've been looking for it and finally put one together so maybe others will find it useful as it is related to the broad question headline.

Related