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?
Lazy 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.