mirror of
https://github.com/hanzoskill/pagerkit.git
synced 2026-10-30 21:05:53 +00:00
3.5 KiB
3.5 KiB
PKPagesView
PKPagesView is the main container view for creating a page-based navigation interface in PagerKit.
Creating a PKPagesView
You create a PKPagesView and provide its content using a @PKPageBuilder closure, where you can list your PKPage instances.
import SwiftUI
import PagerKit
struct ContentView: View {
@State private var currentPage = 0
var body: some View {
PKPagesView {
PKPage { Text("Page 1") }
PKPage { Text("Page 2") }
PKPage { Text("Page 3") }
}
.pkCurrentPageIndex($currentPage)
}
}
Key Modifiers
PKPagesView offers a wide range of modifiers to customize its appearance and behavior.
Pager Behavior
.pkPageNavigationOrientation(_:): Sets the scroll direction to.horizontalor.vertical..pkPageOptions(_:): Provides a dictionary ofUIPageViewController.OptionsKeyfor fine-tuning behavior on UIKit platforms (e.g.,[.interPageSpacing: 20]).
Page Indicator Customization
- Visibility:
.pkPageControlIndicatorHidden(_:): Hides the indicator entirely..pkPageControlIndicatorHidesForSignlePage(_:): Hides the indicator if there's only one page.
- Positioning:
.pkPageControlIndicatorAlignment(_:): Sets the alignment of the indicator (e.g.,.top,.bottom,.leading)..pkPageControlIndicatorAlignment(spacing:alignment:): Sets alignment with custom spacing from the edge.
- Styling:
.pkPageControlIndicatorBackgroundStyle(_:): Sets the indicator's background style (.automatic,.minimal,.prominent)..pkPageControlIndicatorDirection(_:): Sets the layout direction of the dots (.natural,.topToBottom, etc.).
- Color:
.pkPageControlIndicatorTintColor(_:): Sets the color for inactive dots..pkPageControlIndicatorCurrentIndicatorTintColor(_:): Sets the color for the active dot.
- Custom Images:
.pkPageControlIndicatorPreferredIndicatorImage(image:): Sets a default custom image for all inactive dots..pkPageControlIndicatorPreferredCurrentPageIndicatorImage(image:): Sets a default custom image for the active dot..pkPageControlIndicator(_:forPage:): Sets a custom image for the inactive dot of a specific page..pkPageControlIndicatorCurrentIndicator(_:forPage:): Sets a custom image for the active dot of a specific page.
Event Handling & State Binding
.pkCurrentPageIndex(index:): Creates a two-way binding to a@Statevariable that holds the current page index..pkOnManualPageChange(action:): A callback for when the user manually swipes or taps to change the page..pkOnAutoPageChange(action:): A callback for when the page changes automatically (e.g., using the.pkPageDurationmodifier on aPKPage)..pkOnTransitionStart(action:): A callback fired when a page transition begins..pkOnTransitionEnd(action:): A callback fired when a page transition completes.
Best Practices
- Use
@Statefor the current page: Bind the pager to a@Statevariable to control it programmatically or react to changes. - Combine modifiers: Chain multiple modifiers to achieve the exact look and feel you want.
- Use platform-specific modifiers: Use
#if os(...)to apply modifiers that are only available on certain platforms (likepkPageOptions). - Leverage event callbacks: Use the
pkOn...modifiers to add analytics, trigger haptic feedback, or perform other actions in response to navigation.