How it works
A TabView presents top-level views in a tabbed interface, letting people switch between peer sections of your app by selecting items along a tab bar. Reach for it when your content divides into a handful of distinct, equally important areas — a home feed, a search screen, a profile — that each deserve their own persistent destination rather than a hierarchical drill-down. The view manages the bar, the selected tab, and the transition between pages for you, so you supply the content and describe each tab's appearance.
Build the tabs in the content closure
TabView's trailing closure is a view builder: each top-level view you place inside becomes one tab, in order. Here the three
Textviews —Text("Your home feed"),Text("Search anything"), andText("Your profile")— are the page content for three tabs.Describe each tab with tabItem(_:)
Apply the
.tabItemmodifier to a tab's content view to declare how that tab appears in the bar. The closure returns the bar entry; TabView reads it to render the tab and otherwise stays out of the way of the page content itself.Use Label for a tab's icon and title
A tab item is typically a
Labelpairing text with an SF Symbol, which TabView lays out as the standard icon-over-title bar entry. The example marks its tabs withLabel("Home", systemImage: "house"),Label("Search", systemImage: "magnifyingglass"), andLabel("Profile", systemImage: "person.circle").Let the bar track the selected tab
With no binding supplied, TabView manages selection internally — tapping a tab item swaps in that tab's page and highlights its
Label. To drive or observe the active tab from your own state, use the initializer that takes aselectionbinding and tag each tab.
Text view with its own .tabItem { Label("Settings", systemImage: "gearshape") } and watch TabView grow the bar to a fourth tab automatically.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 TabViewDemo: View {
var body: some View {
TabView {
Text("Your home feed")
.tabItem {
Label("Home", systemImage: "house")
}
Text("Search anything")
.tabItem {
Label("Search", systemImage: "magnifyingglass")
}
Text("Your profile")
.tabItem {
Label("Profile", systemImage: "person.circle")
}
}
}
}