@@ -45,22 +45,26 @@ Alternatively, if you're unable to use SPM for some reason, you can import it us
4545import ASCollectionView
4646import SwiftUI
4747
48- struct SingleSectionExampleView : View {
49- @State var dataExample = (0 ..< 30 ).map { $0 }
50-
51- var body: some View
52- {
53- ASCollectionView (data : dataExample, dataID : \.self ) { item, _ in
54- Color.blue
55- .overlay (Text (" \( item ) " ))
56- }
57- .layout {
58- .grid (layoutMode : .adaptive (withMinItemSize : 100 ),
59- itemSpacing : 5 ,
60- lineSpacing : 5 ,
61- itemSize : .absolute (50 ))
62- }
63- }
48+ struct SingleSectionExampleView : View
49+ {
50+ @State var dataExample = (0 ..< 30 ).map { $0 }
51+
52+ var body: some View
53+ {
54+ ASCollectionView (data : dataExample, dataID : \.self )
55+ { item, _ in
56+ Color.blue
57+ .overlay (Text (" \( item ) " ))
58+ }
59+ .layout
60+ {
61+ .grid (
62+ layoutMode : .adaptive (withMinItemSize : 100 ),
63+ itemSpacing : 5 ,
64+ lineSpacing : 5 ,
65+ itemSize : .absolute (50 ))
66+ }
67+ }
6468}
6569```
6670
@@ -71,64 +75,69 @@ Below is an example of how to include a collection view with two sections (each
7175import SwiftUI
7276import ASCollectionView
7377
74- struct ExampleView : View {
78+ struct ExampleView : View
79+ {
7580 @State var dataExampleA = (0 ..< 21 ).map { $0 }
7681 @State var dataExampleB = (0 ..< 15 ).map { " ITEM \( $0 ) " }
7782
7883 var body: some View
7984 {
8085 ASCollectionView
81- {
82- ASCollectionViewSection (
83- id : 0 ,
84- data : dataExampleA,
85- dataID : \.self )
86- { item, _ in
87- Color.blue
88- .overlay (
89- Text (" \( item ) " )
90- )
91- }
92- ASCollectionViewSection (
93- id : 1 ,
94- data : dataExampleB,
95- dataID : \.self )
96- { item, _ in
97- Color.green
98- .overlay (
99- Text (" Complex layout - \( item ) " )
100- )
101- }
102- .sectionHeader
103- {
104- Text (" Section header" )
105- .padding ()
106- .frame (maxWidth : .infinity , alignment : .leading ) // Fill width and align text to the left
107- .background (Color.yellow )
108- }
109- .sectionFooter
110- {
111- Text (" This is a section footer!" )
112- .padding ()
113- }
114- }
115- .layout { sectionID in
116- switch sectionID {
117- case 0 :
118- // Here we use one of the provided convenience layouts
119- return .grid (layoutMode : .adaptive (withMinItemSize : 100 ),
120- itemSpacing : 5 ,
121- lineSpacing : 5 ,
122- itemSize : .absolute (50 ))
123- default :
124- return ASCollectionLayoutSection { environment in
125- // ...
126- // You could return any custom NSCollectionLayoutSection here. For an example see this file: /readmeAssets/SampleUsage.swift
127- // ...
128- }
129- }
130- }
131- }
86+ {
87+ ASCollectionViewSection (
88+ id : 0 ,
89+ data : dataExampleA,
90+ dataID : \.self )
91+ { item, _ in
92+ Color.blue
93+ .overlay (
94+ Text (" \( item ) " )
95+ )
96+ }
97+ ASCollectionViewSection (
98+ id : 1 ,
99+ data : dataExampleB,
100+ dataID : \.self )
101+ { item, _ in
102+ Color.green
103+ .overlay (
104+ Text (" Complex layout - \( item ) " )
105+ )
106+ }
107+ .sectionHeader
108+ {
109+ Text (" Section header" )
110+ .padding ()
111+ .frame (maxWidth : .infinity , alignment : .leading ) // Fill width and align text to the left
112+ .background (Color.yellow )
113+ }
114+ .sectionFooter
115+ {
116+ Text (" This is a section footer!" )
117+ .padding ()
118+ }
119+ }
120+ .layout
121+ { sectionID in
122+ switch sectionID
123+ {
124+ case 0 :
125+ // Here we use one of the provided convenience layouts
126+ return .grid (
127+ layoutMode : .adaptive (withMinItemSize : 100 ),
128+ itemSpacing : 5 ,
129+ lineSpacing : 5 ,
130+ itemSize : .absolute (50 ))
131+ default :
132+ return ASCollectionLayoutSection
133+ { environment in
134+ // ...
135+ // You could return any custom NSCollectionLayoutSection here. For an example see this file: /readmeAssets/SampleUsage.swift
136+ // ...
137+ }
138+ }
139+ }
140+ }
132141}
133142```
134143
@@ -139,20 +148,21 @@ ASCollectionView has support for supplementary views. To add a supplementary vie
139148
140149``` swift
141150ASCollectionViewSection (... ) { ... }
142- .sectionHeader
143- {
144- Text (" Section header" )
145- .background (Color.yellow )
146- }
147- .sectionFooter
148- {
149- Text (" Section footer" )
150- .background (Color.blue )
151- }
152- .sectionSupplementary (ofKind : " someOtherSupplementaryKindRequestedByYourLayout" ) {
153- Text (" Section supplementary" )
154- .background (Color.green )
155- }
151+ .sectionHeader
152+ {
153+ Text (" Section header" )
154+ .background (Color.yellow )
155+ }
156+ .sectionFooter
157+ {
158+ Text (" Section footer" )
159+ .background (Color.blue )
160+ }
161+ .sectionSupplementary (ofKind : " someOtherSupplementaryKindRequestedByYourLayout" )
162+ {
163+ Text (" Section supplementary" )
164+ .background (Color.green )
165+ }
156166```
157167
158168
@@ -166,28 +176,29 @@ Declaring a swift view conforming to `Decoration`:
166176``` swift
167177struct GroupBackground : View , Decoration
168178{
169- let cornerRadius: CGFloat = 12
170- var body: some View
171- {
172- RoundedRectangle (cornerRadius : cornerRadius)
173- .fill (Color (.secondarySystemGroupedBackground ))
174- }
179+ let cornerRadius: CGFloat = 12
180+ var body: some View
181+ {
182+ RoundedRectangle (cornerRadius : cornerRadius)
183+ .fill (Color (.secondarySystemGroupedBackground ))
184+ }
175185}
176186```
177187
178188Registering the decoration type with the layout (ASCollectionLayout):
179189``` swift
180190var layout: ASCollectionLayout<Section>
181191{
182- ASCollectionLayout< Section>
183- {
184- // ... Here is an example of including a decoration in a compositional layout.
185- let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background (elementKind : " groupBackground" )
186- sectionBackgroundDecoration.contentInsets = section.contentInsets
187- section.decorationItems = [sectionBackgroundDecoration]
188- // ...
192+ ASCollectionLayout< Section>
193+ {
194+ // ... Here is an example of including a decoration in a compositional layout.
195+ let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background (elementKind : " groupBackground" )
196+ sectionBackgroundDecoration.contentInsets = section.contentInsets
197+ section.decorationItems = [sectionBackgroundDecoration]
198+ // ...
199+ }
200+ .decorationView (GroupBackground.self , forDecorationViewOfKind : " groupBackground" ) // REGISTER the decoration view type
189201}
190- .decorationView (GroupBackground.self , forDecorationViewOfKind : " groupBackground" ) // REGISTER the decoration view type
191202```
192203
193204
@@ -199,39 +210,45 @@ var layout: ASCollectionLayout<Section>
199210Define layout for all sections:
200211``` swift
201212ASCollectionView (... ) { ... }
202- .layout {
203- ASCollectionLayoutSection { layoutEnvironment in
204- // Construct and return a NSCollectionLayoutSection here
213+ .layout
214+ {
215+ ASCollectionLayoutSection
216+ { layoutEnvironment in
217+ // Construct and return a NSCollectionLayoutSection here
218+ }
205219 }
206- }
207220```
208221
209222Define layout per section:
210223``` swift
211224ASCollectionView (... ) { ... }
212- .layout { sectionID in
213- switch sectionID {
214- case .userSection :
215- return ASCollectionLayoutSection { layoutEnvironment in
216- // Construct and return a NSCollectionLayoutSection here
225+ .layout
226+ { sectionID in
227+ switch sectionID
228+ {
229+ case .userSection :
230+ return ASCollectionLayoutSection
231+ { layoutEnvironment in
232+ // Construct and return a NSCollectionLayoutSection here
233+ }
217234 }
218- }
219235 case .postSection :
220- return ASCollectionLayoutSection { layoutEnvironment in
221- // Construct and return a NSCollectionLayoutSection here
236+ return ASCollectionLayoutSection
237+ { layoutEnvironment in
238+ // Construct and return a NSCollectionLayoutSection here
222239 }
223240 }
224- }
225241```
226242
227243Use a custom UICollectionViewLayout:
228244``` swift
229245ASCollectionView (... ) { ... }
230- .layout {
231- let someCustomLayout = CustomUICollectionViewLayout ()
232- someCustomLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
233- return someCustomLayout
234- }
246+ .layout
247+ {
248+ let someCustomLayout = CustomUICollectionViewLayout ()
249+ someCustomLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
250+ return someCustomLayout
251+ }
235252```
236253
237254### Other tips
0 commit comments