2
votes

I have an app with a UITabBarController as my initial view controller.

Currently I'm doing everything in Storyboard but I want to programmatically add a tab to the tab bar based on a user being logged in or not.

I made a TestViewController to test this out. Right now I have two tabs (pictured below). I want to have a third tab positioned on the right programmatically. I put this code in my AppDelegate's didFinishLaunching method. Based on print statements the view controller is being added to the tab bar but it is not appearing in the tab bar then the app loads.

Any suggestions?

    func addTabTEST() {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let tabController = storyboard.instantiateViewControllerWithIdentifier("RootTabController") as! UITabBarController
    let TestVC = storyboard.instantiateViewControllerWithIdentifier("TestViewController") as! TestViewController
    let icon = UITabBarItem(title: "test", image: nil, selectedImage: nil)
    TestVC.tabBarItem = icon

    print("TAB CONTROLLERS 1: \(tabController.viewControllers)")

    tabController.addChildViewController(TestVC)
    tabController.viewControllers![2] = TestVC

    print("TAB CONTROLLERS 2: \(tabController.viewControllers)")

}

enter image description here

enter image description here

3

3 Answers

3
votes
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let nav1 = UINavigationController()
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

    let first: ViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
    nav1.viewControllers = [first]
    nav1.setNavigationBarHidden(true, animated: true)
    nav1.title = "first"

    let nav2 = UINavigationController()
    let second: SecondViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
    nav2.viewControllers = [second]
    nav2.setNavigationBarHidden(true, animated: true)
    nav2.title = "second"

    let nav3 = UINavigationController()
    let third: ThirdViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ThirdViewController") as! ThirdViewController
    nav3.viewControllers = [third]
    nav3.setNavigationBarHidden(true, animated: true)
    nav3.title = "third"



    let tabController = UITabBarController()
    tabController.viewControllers = [nav1,nav2,nav3]
    tabController.selectedIndex = 0

    self.window!.rootViewController = tabController
    self.window?.makeKeyAndVisible()
0
votes

this is for swift 4

self.window = UIWindow(frame: UIScreen.main.bounds) let nav1 = UINavigationController() let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

    let first = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
    nav1.viewControllers = [first]
    nav1.setNavigationBarHidden(true, animated: true)
    nav1.title = "first"

    let nav2 = UINavigationController()
    let second: HomeViewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController2") as! HomeViewController2
    nav2.viewControllers = [second]
    nav2.setNavigationBarHidden(true, animated: true)
    nav2.title = "second"

    let nav3 = UINavigationController()
    let third = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController3") as! HomeViewController3
    nav3.viewControllers = [third]
    nav3.setNavigationBarHidden(true, animated: true)
    nav3.title = "third"



    let tabController = UITabBarController()
    tabController.viewControllers = [nav1,nav2,nav3]
    tabController.selectedIndex = 0

    self.window!.rootViewController = tabController
    self.window?.makeKeyAndVisible()
0
votes

if you do not want to use UIStoryboard and if you have three view controllers named oneVC, twoVC, and threeVC you can use (works on Swift 5.3 with iOS 14.2)

        let window = UIWindow(frame: UIScreen.main.bounds)
        window.backgroundColor = .systemBackground
        self.window = window

        // Put image path if you want to have an image on your TabBar for this view controller
        self.oneVC?.tabBarItem = UITabBarItem(title: "One", image: nil, selectedImage: nil)
        self.twoVC?.tabBarItem = UITabBarItem(title: "Two", image: nil, selectedImage: nil)
        self.threeVC?.tabBarItem = UITabBarItem(title: "Three", image: nil, selectedImage: nil)

        let tabController = UITabBarController()
        tabController.viewControllers = [oneVC, twoVC, threeVC]
        tabController.selectedIndex = 0

        self.window!.rootViewController = tabController
        self.window?.makeKeyAndVisible()