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.
Place each Tab inside a TabView
Tabis only meaningful as a child of aTabView, which collects its tabs and renders the bar that switches between them. In the example, threeTabvalues sit directly in theTabView(selection:)builder, and each one becomes one entry in the tab bar.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. HereTab("Home", systemImage: "house", ...)shows "Home" beneath thehousesymbol, while the other tabs pair"Search"withmagnifyingglassand"Profile"withperson.circle.Identify the tab with a value
The
valueparameter is the tag that the enclosingTabView's selection binding compares against to decide which tab is active. The three tabs use the values1,2, and3, matching the type of@State private var selection, so the binding can resolve to exactly one of them.Supply the destination in the content closure
The trailing
contentclosure holds the view shown when this tab is selected, scoped to that tab alone. In the example eachTabreturns aTextdestination such asText("Home")withfont(.largeTitle)andpadding(), so switching tabs swaps the whole content area.Connect the value to the selection binding
Because each
Tabcarries avalue, theTabViewcan be created withTabView(selection: $selection)and stay in sync with your state. Settingselectionprogrammatically activates theTabwhosevaluematches, and tapping a tab writes itsvalueback into the binding.
@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.
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()
}
}
}
}