SwiftUI + Xcode 12

SwiftUI | Implement a TabView With TabItem()

Let’s see how to implement a SwiftUI TabView that switch Views each time you click on a TabItem

Sullivan De Carli
Swift Productions

--

SwiftUI logo with a screenshot showing a navigation bar
From the author

Difficulty: Beginner | Easy | Normal | Challenging

Environment : Xcode 12 & SwiftUI

Create a new project

Open Xcode > New Project > App Template > Name it TabViewDemo> SwiftUI App Life Cycle & Interface.

New project from Xcode
Xcode > New Project

Create the 3 Views

Go to your project target > New File > SwiftUI View > name them as follow:

  • FirstView
  • SecondView
  • ThirdView

Now, you will have the following file in your project:

SwiftUi project folder in Xcode

Create the TabView

Head back to your ContentView.swift file and replace the Text UI element with the following code:

Here, we gave our TabView three TabItem() with an icon picked up from Sf Symbol and a name.

Run the project 🏋🏻‍♂️

Now, run your Xcode project and you will be able to switch between Views thanks to the bottom navigation.

Xcode with the simulator running
Final Screenshot

Thanks for reading! I’m always happy to have a chat and collaborate at hello@sullivandecarli.com. Consider subscribing to enjoy unlimited access to my articles and all of Medium through my referral link.

--

--

Sullivan De Carli
Swift Productions

Consultant in iOS Dev at Deloitte. I write about Design & App development. Let’s have a chat at hello@sullivandecarli.com