Ask Your Question
2

What is the method to programmatically include tabs in a UITabBarController using Swift?

asked 2023-07-01 10:21:48 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-01 10:42:02 +0000

scrum gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-01 10:21:48 +0000

Seen: 9 times

Last updated: Jul 01 '23