Adding a UITabBarController To a Swift Project

Can be tricky in iOS15

Steven Curtis
4 min readMay 31

--

For so long I’d only implemented UITabBarController using a storyboard. Can I do it now, programmatically? Must be possible.

Before we begin

Difficulty: Beginner | Easy | Normal | Challenging

This article has been developed using Xcode 14.2, and Swift 5.7.2

Terminology:

SwiftUI: A simple way to build user interfaces across Apple platforms

Prerequisites:

  • You will be expected to be aware how to make a SwiftUI project

Using Storyboards

I remember doing this now. There isn’t any code but it’s still quite hard to see anything

If I remember it was pretty trivial to add a tabbarcontroller to the scene

It’s more than possible to change the text on the item by using the attributes inspector, but it’s all fairly trivial.

It all works a-ok.

But what about my usual way to create a project? Using iOS 15?

Using a Programmatic Method

All of the shenanigans are going to take place in the scene(_:willConnectTo:options:) function in the SceneDelegate. I’ll give you the completed code and then walk you though:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }

let tabBarController = UITabBarController()

let firstViewController = ViewController()
firstViewController.title = "First"
let…

--

--