Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Last active April 18, 2021 15:13
Show Gist options
  • Save prafullakumar/777415b703e6b40e1c22464f60a0d45f to your computer and use it in GitHub Desktop.
Save prafullakumar/777415b703e6b40e1c22464f60a0d45f to your computer and use it in GitHub Desktop.
import SwiftUI
struct StickyLayoutGrid: View {
let columns: [GridItem] = Array(repeating: .init(.flexible()), count: 3)
var body: some View {
NavigationView {
ScrollView(.vertical) { //set .horizontal to scroll horizontally
///pinnedViews: [.sectionHeaders] => set header or footer you want to pin. if do not want to pin leave empty, sectionFooters => for sticky footer
LazyVGrid(columns: columns, spacing: 10, pinnedViews: [.sectionHeaders]) {
Section(header: headerView(type: "Grid")) {
ForEach(0..<10) { i in
Text("Grid: \(i)").frame(width: 100, height: 100, alignment: .center).background(Color.gray).cornerRadius(10.0)
}
}
}
LazyVStack(spacing: 3, pinnedViews: [.sectionHeaders]) {
Section(header: headerView(type: "List")) {
ForEach(0..<15) { i in
HStack {
Spacer()
Text("List: \(i)")
Spacer()
}.padding(.all, 30).background(Color.gray)
}
}
}
}
.navigationTitle("Sticky Header")
}
}
func headerView(type: String) -> some View{
return HStack {
Spacer()
Text("Section header \(type)")
Spacer()
}.padding(.all, 10).background(Color.blue)
}
}
struct StickyLayoutGrid_Previews: PreviewProvider {
static var previews: some View {
StickyLayoutGrid()
}
}
@grgar
Copy link

grgar commented Jul 3, 2020

Apple uses Array init(repeating:count:) in LazyVGrid documentation and I think that's cleaner (also typo in name).

-     let collums = [
-         /// define number of caullum here
-         GridItem(.flexible()),
-         GridItem(.flexible()),
-         GridItem(.flexible())
-     ]
+    let columns: [GridItem] = Array(repeating: .init(.flexible()), count: 3)

@prafullakumar
Copy link
Author

Thanks for commenting. Updated the gist

Apple uses Array init(repeating:count:) in LazyVGrid documentation and I think that's cleaner (also typo in name).

-     let collums = [
-         /// define number of caullum here
-         GridItem(.flexible()),
-         GridItem(.flexible()),
-         GridItem(.flexible())
-     ]
+    let columns: [GridItem] = Array(repeating: .init(.flexible()), count: 3)

@jrobinsonadrenaline
Copy link

Spelling error on line 8

LazyVGrid(columns: collums, spacing: 10, pinnedViews: [.sectionHeaders])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment