@@ -12,16 +12,14 @@ import SwiftUI
12
12
import UIKit
13
13
#endif
14
14
15
- /**
16
- This view can be used to render a collection of pages, that
17
- can be swiped through horizontally.
18
-
19
- This will use a regular `TabView` on iOS, and a custom page
20
- view on other platforms.
21
-
22
- Apply the `.onboardingPageViewStyle(...)` modifier to style
23
- this view.
24
- */
15
+ /// This view can be used to show a collection of pages that
16
+ /// can be swiped through horizontally.
17
+ ///
18
+ /// This view uses a regular SwiftUI `TabView` on iOS, and a
19
+ /// custom page view implementation on other platforms.
20
+ ///
21
+ /// Apply an ``SwiftUI/View/onboardingPageViewStyle(_:)`` to
22
+ /// your view to customize its style.
25
23
public struct OnboardingPageView < Page, PageItemView: View > : View {
26
24
27
25
/// Create a tutorial page view.
@@ -51,7 +49,7 @@ public struct OnboardingPageView<Page, PageItemView: View>: View {
51
49
52
50
public var body : some View {
53
51
bodyContent
54
- . task { setupAppearance ( ) }
52
+ . onAppear { setupAppearance ( ) }
55
53
}
56
54
}
57
55
@@ -72,7 +70,7 @@ private extension OnboardingPageView {
72
70
. tag ( $0. offset)
73
71
}
74
72
}
75
- . tabViewStyle ( . page)
73
+ . tabViewStyle ( . page( indexDisplayMode : . always ) )
76
74
#else
77
75
PageView (
78
76
pages: Array ( pages. enumerated ( ) ) ,
@@ -99,8 +97,32 @@ private extension OnboardingPageView {
99
97
func setupAppearance( ) {
100
98
#if os(iOS)
101
99
let appearance = UIPageControl . appearance ( )
102
- appearance. currentPageIndicatorTintColor = UIColor ( style. currentPageIndicatorTintColor)
103
100
appearance. pageIndicatorTintColor = UIColor ( style. pageIndicatorTintColor)
101
+ appearance. currentPageIndicatorTintColor = UIColor ( style. currentPageIndicatorTintColor)
104
102
#endif
105
103
}
106
104
}
105
+
106
+ #Preview {
107
+
108
+ struct Preview : View {
109
+
110
+ @State
111
+ private var index = 0
112
+
113
+ var body : some View {
114
+ OnboardingPageView ( pages: Array ( 0 ... 10 ) , pageIndex: $index) { index, info in
115
+ Text ( " Page \( index) / \( info. totalPageCount) " )
116
+ }
117
+ . onboardingPageViewStyle (
118
+ . init(
119
+ pageIndicatorTintColor: . red,
120
+ currentPageIndicatorTintColor: . gray
121
+ )
122
+ )
123
+ . background ( Color . blue)
124
+ }
125
+ }
126
+
127
+ return Preview ( )
128
+ }
0 commit comments