Skip to content

SwiftUI: how to create a Tab View

New Course Coming Soon:

Get Really Good at Git

It’s common in iOS apps to use a Tab View. The one with a few choices at the bottom, and you can completely switch what’s in the screen by tapping the icon / label.

SwiftUI conveniently provides us a view called TabView, which makes it easy to implement such a UI pattern.

Here’s the simplest possible example of a TabView:

import SwiftUI

struct ContentView: View {
    
    var body: some View {
        TabView {
            Text("First")
                .tabItem {
                    Label("First", systemImage: "tray")
                }

            Text("Second")
                .tabItem {
                    Label("Second", systemImage: "calendar")
                }
        }
    }
}

And here’s the result:

See? We have a TabView view, and inside it, we have 2 views.

Both are Text views to make it simple.

Their tabItem modifier will add them to the TabView with a label provided as a Label view.

Of course you will want to use a custom view instead of Text in most cases.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching May 21, 2024. Join the waiting list!
→ Get my Swift Handbook

Here is how can I help you: