mirror of
https://github.com/hanzoskill/pagerkit.git
synced 2026-10-30 21:05:53 +00:00
2.2 KiB
2.2 KiB
PKPageBuilder
PKPageBuilder is a result builder that allows you to declaratively construct a list of PKPage components for a PKPagesView.
Overview
You don't use PKPageBuilder directly. Instead, it's the technology that powers the closure-based syntax of PKPagesView. It allows you to mix and match different ways of creating pages in a natural, SwiftUI-like way.
Features
PKPageBuilder supports:
- Multiple pages: Simply list your
PKPageviews. - Conditional pages: Use
ifandif-elsestatements. - Optional pages: Use
if letto include pages based on optional data. - Platform-specific pages: Use
#iforif #available. - Pages from arrays:
ForEachis also supported through the builder.
Examples
Mixing Static and Conditional Pages
struct ContentView: View {
@State private var showExtraPage = true
var body: some View {
PKPagesView {
PKPage { Text("Welcome") }
if showExtraPage {
PKPage { Text("An Extra Page") }
}
if #available(iOS 16.0, *) {
PKPage { Text("This page is only for iOS 16+") }
} else {
PKPage { Text("This is a fallback for older iOS versions") }
}
}
}
}
Pages from a ForEach
PKPageBuilder works with ForEach to dynamically generate pages from a collection.
struct ContentView: View {
let items = ["First", "Second", "Third"]
var body: some View {
PKPagesView {
ForEach(items, id: \.self) { item in
PKPage { Text("Page: \(item)") }
}
}
}
}
How It Works
PKPageBuilder has a set of static methods that the Swift compiler uses to transform the declarative code inside the PKPagesView closure into a single array of PKPage objects. These methods include:
buildBlock(...): Combines multiple pages into one list.buildEither(first:)andbuildEither(second:): Handlesif-elsestatements.buildOptional(_:): Handlesif let.buildLimitedAvailability(_:): Handles#availablechecks.buildExpression(_:): Handles a single page expression.
You don't need to call these methods yourself; the compiler does it for you.