SwiftUI TabView: Create and Customize a Tab Bar

Short answer: in SwiftUI you create a tab bar with TabView. Put each screen inside the TabView and attach .tabItem { } to it with a Label for the title and SF Symbol icon. Bind a selection state to switch tabs from code.

Basic TabView

import SwiftUI

struct ContentView: View {
    var body: some View {
        TabView {
            HomeView()
                .tabItem {
                    Label("Home", systemImage: "house")
                }

            SearchView()
                .tabItem {
                    Label("Search", systemImage: "magnifyingglass")
                }

            ProfileView()
                .tabItem {
                    Label("Profile", systemImage: "person")
                }
        }
    }
}

struct HomeView: View {
    var body: some View { Text("Home").font(.largeTitle) }
}

struct SearchView: View {
    var body: some View { Text("Search").font(.largeTitle) }
}

struct ProfileView: View {
    var body: some View { Text("Profile").font(.largeTitle) }
}

The tab bar appears at the bottom on iPhone. Names such as house and person are SF Symbols, Apple’s built-in icon set.

Control the selected tab

Store the selected tab in state and give each tab a tag:

struct ContentView: View {
    @State private var selection = 0

    var body: some View {
        TabView(selection: $selection) {
            HomeView()
                .tabItem { Label("Home", systemImage: "house") }
                .tag(0)

            SearchView()
                .tabItem { Label("Search", systemImage: "magnifyingglass") }
                .tag(1)

            ProfileView()
                .tabItem { Label("Profile", systemImage: "person") }
                .tag(2)
        }
    }
}

Now you can switch tabs with code, for example selection = 2 inside a button action. For clearer code, use an enum for the tags instead of numbers.

Add a badge

ProfileView()
    .tabItem { Label("Profile", systemImage: "person") }
    .badge(3)

Use .badge("New") for text or a number for counts. A badge of 0 is hidden.

Change the tab bar color

TabView {
    // tabs here
}
.tint(.orange)

.tint sets the color of the selected tab. On older iOS versions, use .accentColor(.orange).

Customize the tab bar appearance

For a custom background, use UITabBarAppearance:

struct ContentView: View {
    init() {
        let appearance = UITabBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = UIColor.systemBackground
        UITabBar.appearance().standardAppearance = appearance
        UITabBar.appearance().scrollEdgeAppearance = appearance
    }

    var body: some View {
        TabView {
            // tabs here
        }
    }
}

Setting scrollEdgeAppearance too keeps the bar the same when the content scrolls to the edge.

Use NavigationStack inside each tab

Each tab should have its own navigation stack, so navigation in one tab does not affect the others:

TabView {
    NavigationStack {
        HomeView()
            .navigationTitle("Home")
    }
    .tabItem { Label("Home", systemImage: "house") }

    NavigationStack {
        SearchView()
            .navigationTitle("Search")
    }
    .tabItem { Label("Search", systemImage: "magnifyingglass") }
}

Put the TabView at the top and the NavigationStacks inside it, not the other way around. NavigationStack needs iOS 16 or later; use NavigationView on older versions.

Swipeable pages instead of a tab bar

TabView {
    Text("Page 1")
    Text("Page 2")
    Text("Page 3")
}
.tabViewStyle(.page)
.indexViewStyle(.page(backgroundDisplayMode: .always))

This turns the TabView into a swipeable pager with dots, useful for onboarding screens.

Newer Tab syntax

Recent iOS versions (iOS 18 and later) add a Tab type that replaces .tabItem in new code, for example Tab("Home", systemImage: "house") { HomeView() }. Check Apple’s documentation for the exact availability, and keep using .tabItem if you support older iOS versions.

Common mistakes

  • Using selection without tags. The binding does nothing unless each tab has a matching .tag.
  • Putting the TabView inside a NavigationStack. You get one shared navigation bar and odd behavior. Use one stack per tab.
  • Creating too many tabs. Apple’s guidance is about five on iPhone; extra tabs go under a “More” tab.
  • Doing heavy work in a tab’s view initializer. Load data when the view appears with .task.

FAQ

How do I create a tab bar in SwiftUI?

Use TabView and add .tabItem with a Label to each child view.

How do I change the selected tab in code?

Bind TabView(selection:) to a @State value and give each tab a matching .tag.

How do I change the tab bar color in SwiftUI?

Use .tint(_:) for the selected item color, or UITabBarAppearance for the background.

How do I hide the tab bar?

On a pushed screen, use .toolbar(.hidden, for: .tabBar) on iOS 16 and later.

More: iOS tutorials and Android tutorials for beginners.