TechnologiesSwiftUI

Tab struct

iOSmacOStvOSwatchOSvisionOSiOS 18.0+✓ renders

The content for a tab and the tab's associated tab item in a tab view.

How it works

A Tab represents a single destination within a TabView, pairing the content a person sees with the label and value that identify it in the tab bar. Use it to declare each section of a tab-based interface as a first-class element rather than attaching tabItem(_:) and tag(_:) modifiers to a plain view. Reach for Tab whenever you build a TabView and want its selection, label, and content to be described together, especially when you drive the active tab with a binding.

  1. Place each Tab inside a TabView

    Tab is only meaningful as a child of a TabView, which collects its tabs and renders the bar that switches between them. In the example, three Tab values sit directly in the TabView(selection:) builder, and each one becomes one entry in the tab bar.

  2. Give the tab a title and a symbol label

    The init(_:systemImage:value:content:) initializer takes a localized title string and an SF Symbol name that together form the tab's label in the bar. Here Tab("Home", systemImage: "house", ...) shows "Home" beneath the house symbol, while the other tabs pair "Search" with magnifyingglass and "Profile" with person.circle.

  3. Identify the tab with a value

    The value parameter is the tag that the enclosing TabView's selection binding compares against to decide which tab is active. The three tabs use the values 1, 2, and 3, matching the type of @State private var selection, so the binding can resolve to exactly one of them.

  4. Supply the destination in the content closure

    The trailing content closure holds the view shown when this tab is selected, scoped to that tab alone. In the example each Tab returns a Text destination such as Text("Home") with font(.largeTitle) and padding(), so switching tabs swaps the whole content area.

  5. Connect the value to the selection binding

    Because each Tab carries a value, the TabView can be created with TabView(selection: $selection) and stay in sync with your state. Setting selection programmatically activates the Tab whose value matches, and tapping a tab writes its value back into the binding.

Try it — Change the initial @State private var selection = 1 to 3 and the interface will open on the Profile tab, since 3 matches that Tab's value.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

Tab.swift
struct TabDemo: View {
    @State private var selection = 1
    var body: some View {
        TabView(selection: $selection) {
            Tab("Home", systemImage: "house", value: 1) {
                Text("Home")
                    .font(.largeTitle)
                    .padding()
            }
            Tab("Search", systemImage: "magnifyingglass", value: 2) {
                Text("Search")
                    .font(.largeTitle)
                    .padding()
            }
            Tab("Profile", systemImage: "person.circle", value: 3) {
                Text("Profile")
                    .font(.largeTitle)
                    .padding()
            }
        }
    }
}
Live preview
Home Home Home Search Profile
Home Home Home Search Profile
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →