49

How to create programmatically tabs from any class extended by UIViewController:

class DashboardTabBarController: UITabBarController {

    override func viewDidLoad() {
        //here

    }
 ...

}
gellezzz
  • 1,165
  • 2
  • 12
  • 21
  • 1
    Have you looked at the [documentation](https://developer.apple.com/library/ios/documentation/uikit/reference/uitabbarcontroller_class/index.html)? – Ron Fessler Nov 10 '14 at 19:08

3 Answers3

88

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;
    }
}
mauricioconde
  • 5,032
  • 3
  • 28
  • 24
  • Hello! I made like this, but when tabbarcontroller is loaded it doesn't highlight uitabbaritem icon – Alexander Khitev Sep 06 '16 at 13:12
  • 1
    @Alexsander add "self.selectedIndex = 0" at the bottom of viewWillAppear() – Neil Aug 23 '17 at 19:35
  • 1
    Adding VCs in `ViewDidLoad` also works, I wonder why you preferred to use `viewWillAppear`! – Husam Jan 19 '18 at 17:17
  • 2
    I have tried same but not showing tab bar. Is there anything other than this which I need to do ? `DashboardTabBarController` -> `viewWillAppear` is not calling. – Ajay Mar 13 '20 at 10:36
  • Creating the view controllers for the tabs should not be done in `viewWillAppear`. `viewDidLoad` is a much better choice. Also, no need to create new instances of `UITabBarItem`. Each view controller already has one. Just set its properties. – HangarRash Apr 02 '23 at 04:19
12

If you are using storyboard for the viewcontrollers then you have to write like this in your tabbarcontroller class.

class CustomTabbarController : UITabBarController {

    override func viewDidLoad() {

        super.viewDidLoad()

        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        let firstViewController = FirstViewController()
        let navigationController = UINavigationController(rootViewController: firstViewController)
        navigationController.title = "First"
        navigationController.tabBarItem.image = UIImage.init(named: "map-icon-1")

       viewControllers = [navigationController]

        if let secondViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController {

            let navgitaionController1 = UINavigationController(rootViewController: secondViewController)
            navgitaionController1.title = "Second"
            navgitaionController1.tabBarItem.image = UIImage.init(named: "second-icon-1")
            var array = self.viewControllers
            array?.append(navgitaionController1)
            self.viewControllers = array

        }

    }
}
kalyani puvvada
  • 496
  • 4
  • 12
  • Do not set the title property on the navigation controllers. Set the title property of the view controllers. If you want the tabs to have a specific title, set the title of the associated `tabBarItem` (just like for the image). – HangarRash Apr 02 '23 at 21:05
-1
    private lazy var tabbarViewController: UITabBarController = {
            let tabbarViewController = UITabBarController()
            tabbarViewController.setViewControllers([startVC,
                                                     offerVC,
                                                     benefitsVC,
                                                     shopVC,
                                                     recipesVC], animated: true)
            
            return tabbarViewController
        }()

window?.rootViewController = tabbarViewController
Wasim
  • 921
  • 12
  • 14