Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI main list scrollable header view without sections?

Is there a way to put a view in the list header without sections? The table views normally have a property called tableHeaderView which is a header for the whole table and has nothing to do with section headers.

I'm trying to have a list and be able to scroll it like tableHeaderView from UITableView.

struct MyView: View {
    
    let myList: [String] = ["1", "2", "3"]
    
    var body: some View {
        VStack {
            MyHeader()
            List(myList, id: \.self) { element in
                Text(element)
            }
        }
    }
}
like image 816
P. Sami Avatar asked Mar 02 '23 05:03

P. Sami


1 Answers

Thanks to Asperi. This is my final solution for the table header.

struct MyHeader: View {
    var body: some View {
        Text("Header")
    }
}

struct DemoTableHeader: View {
    let myList: [String] = ["1", "2", "3"]

    var body: some View {
        List {
            MyHeader()
            ForEach(myList, id: \.self) { element in
                Text(element)
            }
        }
    }
}
like image 177
P. Sami Avatar answered Mar 04 '23 23:03

P. Sami