List View within a Scrollview

The bane of my existence has been designing interfaces where the whole view needs to scroll, but a portion is a List and the other portion is static.

I run into this problem time and again so I was hoping someone has a good solution because we all know that embedding a List view inside ScrollView is a no-go within SwiftUI. It simply doesn't work.

So what is a best practice when you need the whole screen to scroll, but a portion is a List? Use a navigation stack instead of a ScrollView? What if it's a child view of a navigation stack already?

Answered by darkpaw in 848479022

Not everything in a List has to be the same thing.

In one of my apps I have a List that has a HeaderView(), then a ToolbarView() then a set of EventRow() views, which looks pretty similar to your wireframe.

Something like this:

VStack {
    List {
        HeaderView()
        ToolbarView()
        ForEach(events.indices, id: \.self) { index in
            EventRow(events[index])
        }
    }
}

Everything scrolls vertically within.

Accepted Answer

Not everything in a List has to be the same thing.

In one of my apps I have a List that has a HeaderView(), then a ToolbarView() then a set of EventRow() views, which looks pretty similar to your wireframe.

Something like this:

VStack {
    List {
        HeaderView()
        ToolbarView()
        ForEach(events.indices, id: \.self) { index in
            EventRow(events[index])
        }
    }
}

Everything scrolls vertically within.

That worked! Not sure what I was doing wrong.

List {
            VStack (alignment: .leading, spacing: 24) {
                HeaderView()
                
                Picker("Filter", selection: $selectedFilter) {
                    Text("One").tag("One")
                    Text("Two").tag("Two")
                }
                .pickerStyle(.segmented)
            }
            .listRowSeparator(.hidden)
            
            if selectedFilter == "One" {
                ForEach(0..<10) { index in
                    NavigationLink(destination: DetailView()) {
                        RowView()
                    }
                }
            }
        }
        .listStyle(.plain)
List View within a Scrollview
 
 
Q