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)")
}