skip to content

In SwiftUI, how do you build a tab bar with TabView and the Tab type, and what did Tab replace?

level: juniorimportance: must knowfreq 65%

answer

  1. a container plus one entry per tab
  2. title, symbol name, content closure
  3. badge sits on the tab entry
  4. iOS 18 type versus older modifier
  5. tabItem plus tag before iOS 18

basics

~20 s

Place one Tab per section inside a TabView: each Tab takes a title, an SF Symbol name and a content closure, and badge(:) adds a count. Tab arrived in iOS 18; earlier code used tabItem(:) and tag(_:) on child views.

solid answer

~40 s

I put a `TabView` at the root of the screen and declare one `Tab` per top-level section, for example `Tab("Inbox", systemImage: "tray") { InboxView() }`. A `.badge(_:)` on the `Tab` shows an unread count. `Tab`, its `TabContentBuilder` and `TabSection` arrived in **iOS 18**; before that each child view carried `.tabItem { Label(...) }` and, for programmatic selection, `.tag(_:)`. The iOS 27 SDK marks `tabItem(_:)` for deprecation in a future release with the message to use the `Tab` initializers, so an app that still deploys to iOS 17 keeps `tabItem` behind an availability check or until it raises its minimum. Each tab that drills down gets its own `NavigationStack` inside the `Tab`, so every section keeps its own back stack and title.

code

swift · 20 lines
swift
struct MailRootTabs: View {
    let unreadCount: Int

    var body: some View {
        TabView {
            Tab("Inbox", systemImage: "tray") {
                NavigationStack { InboxView() }
            }
            .badge(unreadCount)

            Tab("Sent", systemImage: "paperplane") {
                NavigationStack { SentView() }
            }

            Tab("Settings", systemImage: "gear") {
                SettingsView()
            }
        }
    }
}

go deeper

for a junior

Recall the shape: a TabView containing one Tab per section, each with a title, a symbol name and content, plus badge(_:) for counts.

for a middle

Explain that Tab is an iOS 18 type built by a TabContentBuilder, and how tabItem(:) and tag(:) did the same job before it.

for a senior

Show the deployment-target judgment: when to keep tabItem behind iOS 17 support, and why each drilling tab owns its own NavigationStack.

for a principal

Frame the tab set as the app's top-level information architecture and plan when raising the minimum to iOS 18 unlocks sections and sidebar adaptation.

## What a tab view is in SwiftUI A **tab view** is a container that switches between several top-level sections of an app with a row of buttons, the tab bar. In SwiftUI the container is `TabView`, available since iOS 13. Apple's documentation describes it as "a view that switches between multiple child views using interactive user interface elements". On iPhone the tab bar sits at the bottom of the screen; from iPadOS 18 it floats at the top over the content. A tab bar is for **peer destinations**: Inbox, Sent, Drafts, Archive and Search in a mail app are all places a person can go at any time. It is not for steps in a flow (that is a navigation stack) or for a master-detail relationship (that is a split view). ## Declaring tabs with `Tab` (iOS 18 and later) Since iOS 18 you declare each section with the `Tab` type inside the `TabView`'s content closure, which is a `TabContentBuilder` rather than an ordinary view builder: - `Tab("Inbox", systemImage: "tray") { InboxView() }` gives a title, an SF Symbol name and the content. - `Tab("Inbox", image: "custom-asset") { ... }` uses an asset image instead of a symbol. - `Tab(value:content:)` and the other `value:` initializers attach a **selection value**, used with `TabView(selection:)` for programmatic switching. - `Tab(value:role:content:)` gives a tab a **role** such as `.search`. Apple's sample notes that you should pass the **outline** variant of a symbol: "The system automatically selects the filled variant when it appears in a tab bar." ## Badges, labels and how many tabs - **Badges** go on the `Tab` itself: `.badge(3)` for a count, or `.badge("!")` for a short string. Apple's `TabView` page shows both. - **Labels** come from the initializer; a custom label is the `Tab(content:label:)` family. - **How many tabs:** the `TabView` documentation advises limiting the number of tabs on iOS and in the iPadOS compact size class "so all tabs fit in the tab bar". ## The older form: `tabItem(_:)` and `tag(_:)` Before iOS 18 every child view of the `TabView` described its own tab with modifiers: | | `Tab` (iOS 18+) | `tabItem(_:)` on a child view | |---|---|---| | Where the label lives | the `Tab` initializer | `.tabItem { Label(...) }` on the content | | Selection value | `value:` parameter | `.tag(_:)` on the content | | Sections and sidebar | `TabSection`, `sidebarAdaptable` | not available | | Roles (search) | `role:` parameter | not available | | Status in the iOS 27 SDK | current | marked for future deprecation | The iOS 27 interface declares `tabItem(_:)` with `deprecated: 100000.0` and the message "Use `Tab(title:image:value:content:)` and related initializers instead". That marker means "deprecated in a future release": the modifier still compiles, but new code on an iOS 18 minimum should use `Tab`. The view-builder `TabView(selection:content:)` initializer is marked the same way, pointing at the `TabContentBuilder`-based ones. For an app whose minimum deployment target is still iOS 17, the honest choice is one of: 1. keep `tabItem(_:)` and `tag(_:)` until the minimum reaches iOS 18, or 2. branch with `if #available(iOS 18, *)` at the root and build both forms, which doubles the tab declaration. ## Where each tab's navigation lives A tab whose content drills down owns a `NavigationStack` **inside** its `Tab`. That way each section keeps its own back stack and its own navigation title, and the `TabView` documentation's own example places the stack inside the tab's content view. Wrapping the whole `TabView` in a single `NavigationStack` gives every tab one shared stack and one navigation bar, which is rarely what a tabbed app wants. How that stack is driven (paths, destinations, pop to root) is the subject of the lesson on stack-based routing. ## Common mistakes - Treating `Tab` as available on iOS 17: it is an iOS 18 type, so an iOS 17 target needs an availability check. - Believing `tabItem(_:)` no longer compiles: it is only marked for a future deprecation. - Putting a badge in the tab's content view instead of on the `Tab`. - Passing filled symbol names instead of the outline variants Apple's sample recommends, since the system picks the filled variant itself. - Using a `TabView` for wizard-like steps that should be a stack or a paged flow.

  • Your app's minimum deployment target is iOS 17. Can you use Tab?
    Not unconditionally. `Tab` is declared `@available(iOS 18.0, ...)`, so on an iOS 17 minimum you either keep `tabItem(_:)` with `tag(_:)` or branch at the root with `if #available(iOS 18, *)` and declare the tabs twice. Most teams keep `tabItem` until they raise the minimum, because the duplicated declaration is easy to let drift.
  • Why does each tab get its own NavigationStack instead of one stack around the TabView?
    A stack inside each `Tab` gives every section its own back stack and navigation title, so drilling into a message in Inbox does not affect Sent. One `NavigationStack` around the whole `TabView` would share a single stack and navigation bar across all tabs, so a pushed screen belongs to no single tab.

A tab bar is like the labelled dividers in a ring binder: each divider opens a different section, the section keeps its own pages, and a sticky note on a divider is the badge.

  • Using Tab in an app that still deploys to iOS 17 without an availability check
  • Claiming tabItem(_:) was removed and no longer compiles in the iOS 27 SDK
  • Wrapping the whole TabView in one NavigationStack shared by every tab
  • Passing filled SF Symbol names instead of the outline variants the tab bar expects
  • Using a TabView for the sequential steps of a sign-up flow

Assumes the iOS 27 SDK; Tab and TabSection need iOS 18, and tabItem(_:) is marked for deprecation in a future release.