1 | initial version |
Here is the code to programmatically include tabs in a UITabBarController using Swift:
// create the view controllers for each tab
let firstViewController = UIViewController()
firstViewController.tabBarItem = UITabBarItem(title: "First", image: UIImage(named: "firstIcon"), selectedImage: nil)
let secondViewController = UIViewController()
secondViewController.tabBarItem = UITabBarItem(title: "Second", image: UIImage(named: "secondIcon"), selectedImage: nil)
let thirdViewController = UIViewController()
thirdViewController.tabBarItem = UITabBarItem(title: "Third", image: UIImage(named: "thirdIcon"), selectedImage: nil)
// create the tab bar controller and set its view controllers array
let tabBarController = UITabBarController()
tabBarController.viewControllers = [firstViewController, secondViewController, thirdViewController]
// set the tab bar controller as the root view controller of the window
window?.rootViewController = tabBarController
In this example, we first create a view controller for each tab and set their tabBarItem
property to show the title and icon for each tab. We then create the UITabBarController
and set its viewControllers
array to the array of view controllers we just created. Finally, we set the UITabBarController
as the root view controller of the window.