Last active
April 18, 2021 15:13
-
-
Save prafullakumar/777415b703e6b40e1c22464f60a0d45f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
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)
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
Apple uses Array
init(repeating:count:)
in LazyVGrid documentation and I think that's cleaner (also typo in name).