Can anyone please tell me how to set the default tab when using storyboards in iOS. I can\'t seem to figure out how to accomplish this.
Thank you
You can achieve this through Xcode 8 or later (just tested it and don't know if it's available before this version)
Do the steps as @Joshua Finch said but:
Provided you're navigating to your UITabBarController from e.g. a login screen it might be least pain to set the selection there:
let tabs = self.storyboard?.instantiateViewController(withIdentifier: "tabs") as! UITabBarController
tabs.selectedIndex = 1
self.present(tabs, animated:false)
In the viewDidLoad()
of the TabBarController
, set selectedIndex
to whatever you want. (0
would be the first, 3
would be the fourth, etc.)
in appdelegate find applicationDidBecomeActive
function and add this lines
let tabBarController = self.window?.rootViewController as! UITabBarController
tabBarController.selectedIndex = 0 // any index you want
You can use one of these two methods:
tabBar.items = tabBarItems;
tabBar.selectedItem = [tabBarItems objectAtIndex:0];
or a direct method from the object
[tabBar setSelectedItem:myUITabBarItem];
or you can combine them to do this:
tabBar.items = tabBarItems;
[tabBar setSelectedItem:[tabBarItems objectAtIndex:0]];
but i havent tested that method yet, hope this helps!
The following code worked for me:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 2;