Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 46 ยท Navigation and App Structure

Tab Views, Sheets, Alerts and Popovers

Learn SwiftUI TabView and every kind of presentation: sheet, full-screen cover, alert, confirmation dialog and popover, driven by state, with detents and dismiss.

A navigation stack takes you deeper into one flow. Real apps also need other kinds of movement: switching between top-level areas (Home, Search, Profile), showing a short task on top of the current screen (a compose form), asking a yes/no question, or showing a menu-like panel next to a button. SwiftUI covers these with TabView for top-level areas and presentations for everything that appears over the current content.

The unifying idea is the same as in the previous lesson: presentation is state. A sheet is on screen because a Bool is true or an optional item is non-nil. To close it, you set the state back, or the system sets it for you when the user swipes down. SwiftUI code is read-only here, and plain-Swift stand-ins model the state logic so you can run it. Read NavigationStack and Navigation Paths first.

Two kinds of movement

Tabs Switch between top-level areas.
Presentations Show something over the current screen.
1 / 2

TabView

A TabView holds several top-level screens and a tab bar to switch between them. Give it a binding for the selection if you want to switch tabs from code.

Swift
enum AppTab: Hashable {
    case home, search, profile
}

struct RootView: View {
    @State private var selection: AppTab = .home

    var body: some View {
        TabView(selection: $selection) {
            Tab("Home", systemImage: "house", value: AppTab.home) {
                NavigationStack { HomeView() }
            }
            Tab("Search", systemImage: "magnifyingglass", value: AppTab.search) {
                NavigationStack { SearchView() }
            }
            Tab("Profile", systemImage: "person", value: AppTab.profile) {
                NavigationStack { ProfileView() }
            }
        }
    }
}

The Tab type is the current way to declare tabs in recent SDKs. Older code attaches .tabItem { Label("Home", systemImage: "house") } and .tag(AppTab.home) to each child; it behaves the same way and you will meet it often.

Two conventions are worth following. Each tab gets its own NavigationStack, so pushing a screen in Search does not affect Home, and each tab keeps its own back stack. And keep tab count small (up to five fits comfortably on an iPhone); more become a "More" list.

The stand-in below models this rule: a selection plus one path per tab. Switching tabs changes which path is visible but never clears the others.

Swift Each tab keeps its own back stack

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

Home still had its two pushed screens when we returned to it, and Search had its own. That is exactly the behavior users expect from a tab bar.

Sheets

A sheet slides up over the current screen and can be dismissed by swiping down. There are two forms.

Swift
struct NotesView: View {
    @State private var isComposing = false
    @State private var selectedNote: Note?      // Note: Identifiable

    var body: some View {
        List(notes) { note in
            Button(note.title) { selectedNote = note }
        }
        .toolbar {
            Button("New", systemImage: "plus") { isComposing = true }
        }
        .sheet(isPresented: $isComposing) {      // Bool-driven
            ComposeView()
        }
        .sheet(item: $selectedNote) { note in    // item-driven
            NoteDetail(note: note)
        }
    }
}

Use isPresented: when the content does not depend on data. Use item: when the sheet shows a specific value: the sheet is visible while the item is non-nil, and the closure receives the non-optional value. This avoids a classic bug where a Bool and a separate "selected" variable fall out of sync, so the sheet opens with stale or missing data.

Inside the presented view, close it with the dismiss environment action instead of reaching back to the parent's state:

Swift
struct ComposeView: View {
    @Environment(\.dismiss) private var dismiss

    var body: some View {
        NavigationStack {
            Form { /* fields */ }
                .navigationTitle("New note")
                .toolbar {
                    ToolbarItem(placement: .cancellationAction) {
                        Button("Cancel") { dismiss() }
                    }
                    ToolbarItem(placement: .confirmationAction) {
                        Button("Save") { /* save */ dismiss() }
                    }
                }
        }
    }
}

Give a sheet that has its own flow its own NavigationStack, as above, so it can have a title bar and toolbar buttons.

Detents and sheet options

Sheets can stop at partial heights. These modifiers go on the content of the sheet:

Swift
.sheet(isPresented: $showFilters) {
    FilterView()
        .presentationDetents([.medium, .large])   // half height or full
        .presentationDragIndicator(.visible)
        .interactiveDismissDisabled(hasUnsavedChanges)
}

presentationDetents accepts built-in sizes (.medium, .large) and custom ones (a fixed height or a fraction of the screen). interactiveDismissDisabled blocks the swipe-down gesture, which is useful when closing would lose the user's input. Always leave an explicit Cancel button when you do that.

Full-screen covers

fullScreenCover has the same two forms as sheet but covers the entire screen and cannot be swiped away. It suits immersive flows such as onboarding, a camera, or a media viewer, and it must offer its own close button, usually calling dismiss.

Swift
.fullScreenCover(isPresented: $showOnboarding) {
    OnboardingView()
}

Use a plain sheet when the content is a short side task; the card look tells users that the screen below is still there.

Alerts and confirmation dialogs

An alert interrupts with a short message and one or more buttons. Use it for errors or serious confirmations, and keep the text short.

Swift
.alert("Delete note?", isPresented: $showDeleteAlert) {
    Button("Delete", role: .destructive) { deleteNote() }
    Button("Cancel", role: .cancel) { }
} message: {
    Text("This cannot be undone.")
}

Roles matter. .destructive styles the button as dangerous, and .cancel marks the button that should be triggered by tapping outside or pressing Escape on supporting platforms. SwiftUI dismisses the alert when any button is tapped.

A confirmation dialog is for choosing among actions related to something the user just did or selected. On iPhone it appears as an action sheet rising from the bottom; on iPad it appears as a popover attached to its source.

Swift
.confirmationDialog("Share this note", isPresented: $showShare, titleVisibility: .visible) {
    Button("Copy link") { }
    Button("Export as PDF") { }
    Button("Cancel", role: .cancel) { }
}

Alerts and dialogs can also be item-driven (presenting:), so the message can use the selected value.

Popovers

A popover is a bubble attached to a control, for short, related content like a color picker or a help tip.

Swift
Button("Info") { showInfo = true }
    .popover(isPresented: $showInfo) {
        Text("Tap and hold a row to reorder it.")
            .padding()
            .presentationCompactAdaptation(.popover)
    }

On regular-width screens (iPad, Mac) a popover looks like a floating bubble. On a compact iPhone layout it adapts to a sheet by default; the presentationCompactAdaptation modifier lets you ask for the popover look instead.

Presentation as state

Because every presentation is driven by state, you can model the rules in plain Swift. The stand-in keeps one Presentation? value and shows that setting a new value replaces the old, that dismiss clears it, and that the item form carries its data with it.

Swift Presentation state: Bool versus item

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

When the detail sheet carries its note inside the case, the data and the visibility can never disagree. That is the idea behind sheet(item:). In a real SwiftUI screen you can use one optional enum like this to drive several presentations, but remember that SwiftUI generally presents one item at a time from a given view, so avoid attaching several presentation modifiers to the same view and opening two at once.

Choosing the right container

  • Switching between parallel areas: TabView.
  • Going deeper into one flow: NavigationStack.
  • A short task or form that can be cancelled: sheet, with its own navigation bar.
  • An immersive or must-finish flow: full-screen cover with an explicit close.
  • A question with a few answers: alert (errors, serious choices) or confirmation dialog (picking an action).
  • A small bubble tied to a control: popover.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does each tab usually contain its own NavigationStack?

    With one stack per tab, pushing a screen in one tab does not change another tab, and switching tabs preserves each back stack.
  2. 2. When is sheet(item:) better than sheet(isPresented:)?

    An item-driven sheet is shown while the optional is non-nil and receives the unwrapped value, so a separate Bool cannot disagree with the data.
  3. 3. What does a button with role .destructive do in an alert?

    Roles tell the system how to style and treat a button. .cancel marks the cancel action.
  4. 4. Which API makes a sheet stop at half height?

    presentationDetents is applied to the sheet's content and lists the heights it may rest at.
  5. 5. A full-screen cover cannot be swiped away. What must you provide?

    Because there is no drag-down gesture, the content needs its own way out.

Summary

  • TabView switches between top-level areas; give each tab its own NavigationStack.
  • Presentations are state: a Bool (isPresented:) or an optional item (item:), with dismiss available in the presented view.
  • Sheets can use presentationDetents and interactiveDismissDisabled; full-screen covers need their own close button.
  • Alerts interrupt with short messages and roles; confirmation dialogs offer related actions; popovers attach to a control.
  • Prefer item-driven presentation when the content depends on a selected value.

Next: NavigationSplitView and iPad Layouts.