I'm attempting to keep a section header on the screen when scrolling through a list but my code is not working. I'm following this Apple tutorial but my code has no effect on the header staying on the screen.
var body: some View {
NavigationSplitView {
List {
LazyVStack(pinnedViews: .sectionHeaders) {
ForEach(searchDates, id: \.self) { date in
Section(header: Text(date)) {
ReceiptSection(searchResults: searchResults, date: date)
}
}.onDelete(perform: deleteItems)
}
}
I have tried moving around where I insert the LazyVStack but that has no effect either. Can anyone tell me how to fix it?
I'm attempting to keep a section header on the screen when scrolling through a list but my code is not working. I'm following this Apple tutorial but my code has no effect on the header staying on the screen.
var body: some View {
NavigationSplitView {
List {
LazyVStack(pinnedViews: .sectionHeaders) {
ForEach(searchDates, id: \.self) { date in
Section(header: Text(date)) {
ReceiptSection(searchResults: searchResults, date: date)
}
}.onDelete(perform: deleteItems)
}
}
I have tried moving around where I insert the LazyVStack but that has no effect either. Can anyone tell me how to fix it?
Share Improve this question asked Jan 18 at 13:15 David LDavid L 4,4293 gold badges20 silver badges32 bronze badges 4 |1 Answer
Reset to default 1Lazy stacks should be inside ScrollView
s, not List
s. In fact, they are not "lazy" unless they are in a ScrollView
. On the other hand, the List
will treat it as one giant list row.
ScrollView {
LazyVStack(pinnedViews: .sectionHeaders) {
ForEach(searchDates, id: \.self) { date in
Section(header: Text(date)) {
ReceiptSection(searchResults: searchResults, date: date)
}
}.onDelete(perform: deleteItems)
}
}
You can also consider using a ListStyle
that has sticky headers. e.g. the .plain
style on iOS. NavigationSplitView
is designed to be driven by the selection of a List
- it navigates to the detail view when a list row is selected.
LazyVStack
should be put inside aScrollView
, notList
. Do you specifically needList
for anything? – Sweeper Commented Jan 18 at 13:25