How do you build a paged onboarding flow with SwiftUI's TabView page style, and how does a Next button move to the next page?
answer
- a tab view style, not a tab bar
- dots come from the page count
- selection binding is the page number
- index view style for the dots
basics
~10 sApply .tabViewStyle(.page) to a TabView of pages: people swipe horizontally and dots show the position. Bind TabView(selection:) to the page value; a Next button changes that value, wrapped in withAnimation so the page slides.
solid answer
~30 sI put the onboarding pages in a `TabView` and apply `.tabViewStyle(.page)`, available on iOS since iOS 14, which replaces the tab bar with horizontal paging and a page indicator. `.page(indexDisplayMode: .always)` or `.never` controls whether the dots show, and `.indexViewStyle(.page(backgroundDisplayMode: .always))` puts a backdrop behind them on light content. To drive it, I bind `TabView(selection: $current)` and give each page a value: on iOS 18 and later with `Tab(value:)`, on iOS 17 with `.tag(_:)`. A Next button does `withAnimation { current += 1 }`, and on the last page calls a completion closure instead.
code
swift · 35 linesstruct OnboardingPage: Identifiable {
let id: Int // 0, 1, 2 ... in display order
let title: String
let symbol: String
}
struct OnboardingView: View {
let pages: [OnboardingPage]
let onFinish: () -> Void
@State private var current = 0
var body: some View {
VStack(spacing: 24) {
TabView(selection: $current) {
ForEach(pages) { page in
Tab(value: page.id) {
Label(page.title, systemImage: page.symbol)
.font(.title)
}
}
}
.tabViewStyle(.page(indexDisplayMode: .always))
.indexViewStyle(.page(backgroundDisplayMode: .always))
Button(current == pages.count - 1 ? "Get Started" : "Next") {
if current < pages.count - 1 {
withAnimation { current += 1 }
} else {
onFinish()
}
}
.buttonStyle(.borderedProminent)
}
}
}go deeper
Know that .tabViewStyle(.page) turns a TabView into swipeable pages with dots, and that a selection binding tracks the page.
Explain indexDisplayMode, indexViewStyle backgrounds, and why each page needs a value matching the selection type.
Show how the pager reports completion to its parent and when a lazy paging scroll view is the better tool.
Keep onboarding short and owned by the app's launch flow, so the pager stays a dumb, reusable component.
## A tab view without a tab bar `TabView` is more than a tab bar. Its **style** decides the presentation, and `PageTabViewStyle`, written `.tabViewStyle(.page)`, turns it into a horizontally **paged** container: each child is one full page, people swipe between them, and a row of dots shows the position. Apple's `TabView` documentation shows a `ForEach` of cities with `.tabViewStyle(.page)` as its paging example. The style has been available on iOS since iOS 14 and is unavailable on macOS. Typical uses are onboarding carousels, image galleries and short "what's new" sequences, all small, fixed sets of pages. ## Controlling the page indicator - `.tabViewStyle(.page(indexDisplayMode: .automatic))` is the default; `.always` and `.never` force the dots on or off. - `.indexViewStyle(.page(backgroundDisplayMode: .always))` draws a backdrop behind the dots, which keeps them legible over light content. The background modes are `.automatic`, `.interactive`, `.always` and `.never`. - The number of dots comes from the number of pages; you do not set it. ## Driving the current page Paging is just **selection**: 1. Hold the current page in state: `@State private var current = 0`. 2. Bind it: `TabView(selection: $current)`. 3. Give each page a value that matches the state's type. On iOS 18 and later, wrap each page in `Tab(value: page.id) { ... }`, where a `ForEach` over the pages produces the tabs. On iOS 17, put `.tag(index)` on each child view instead, using the view-builder `TabView(selection:content:)` that the iOS 27 SDK marks for a future deprecation. 4. A **Next** button changes the state: `withAnimation { current += 1 }`. Wrapping the change in `withAnimation` asks SwiftUI to animate the move; how animations are scoped is its own topic. 5. On the last page, the button calls a completion closure instead of incrementing. Because the binding is two-way, swiping also updates `current`, so the button's label can switch to "Get Started" on the last page. ## Walking through the state Following the example's state through one run makes the mechanism concrete: 1. The view appears with `current == 0`; the first page shows and the indicator marks the first position. 2. Someone swipes to the next page; the pager writes `1` into `current`, and the button still reads Next. 3. Someone taps Next; `withAnimation { current += 1 }` sets `current` to `2`, the last page in a three-page flow. 4. The button label now reads Get Started, because it compares `current` with `pages.count - 1`. 5. Tapping it calls `onFinish()`; the parent ends the flow and records that onboarding is done. Nothing in this sequence reads gesture positions or offsets: the page number is ordinary state, which is also what lets a preview or a test open the pager on any page. ## Where the flow lives The onboarding view does not decide **whether** it shows. The parent does: commonly it presents the pager as a full-screen cover on first launch and records a "has completed onboarding" flag in user defaults. Both of those belong to other topics, modal presentation and stored defaults, and the pager only reports completion through its closure. ## Page style or something else? | Need | Reach for | |---|---| | a handful of full-screen pages with dots | `TabView` with `.tabViewStyle(.page)` | | a long or lazily loaded horizontal feed | a `ScrollView` with paging scroll behaviour | | top-level app sections | a `TabView` with a tab bar | ## Common mistakes - Expecting a selection binding to move pages when the pages carry no value, or carry values of a different type than the state. - Changing the page without `withAnimation`, so it jumps instead of sliding. - Using `.indexViewStyle(.page)` alone and expecting paging; it styles the dots of an already paged view, it does not page anything. - Loading dozens of heavy pages into a paged `TabView` instead of a lazy scroll view. - Letting the pager decide when onboarding is complete instead of reporting to its parent.
- How would you write the same pager for an app that still deploys to iOS 17?Use `TabView(selection: $current)` with ordinary child views and put `.tag(page.id)` on each page view instead of wrapping it in `Tab(value:)`, which needs iOS 18. The page style, the index display mode and the Next button stay the same; the iOS 27 SDK only marks that view-builder initializer for a future deprecation.
- When would you use a paging ScrollView instead of a page-style TabView?When the pages are many or expensive, such as a long horizontal feed, a lazy stack in a `ScrollView` with paging scroll behaviour creates pages on demand. A page-style `TabView` suits a small fixed set like onboarding, where the built-in page indicator and selection binding are exactly what you want.
- Thinking .indexViewStyle(.page) alone makes a TabView page
- Moving pages from a button without any selection binding
- Tagging pages with a different type than the selection state
- Expecting the page style to be available on macOS
- Changing the page index without withAnimation and wondering why it jumps
Assumes the iOS 27 SDK; the page style dates from iOS 14, while the Tab(value:) form shown needs iOS 18 and iOS 17 uses tag(_:).