Tuesday, October 01, 2024

Create a list in SwiftUI with sticky section headers

 Sample Code

import SwiftUI



struct ContentView: View {

    @State var isCustomViewControllerDisplayed = false

    @State private var toggleState = false

    

    var body: some View {

        HStack{

            List {

                Text("Non-sticky part of the header...")

                    .font(.system(size: 32))

                

                Section(header: HStack {

                    Text("Sticky header")

                        .font(.system(size: 24))

                    Spacer()

                    Image(systemName: "sun.max.fill")

                }) {

                    ForEach(1..<40) { index in

                        HStack {

                            Text("Row #\(index)")

                            Spacer()

                            Image(systemName: "sun.max.fill")

                           

                        }

                    }

                }

                Section(header: HStack {

                    Text("Sticky header1")

                        .font(.system(size: 24))

                    Spacer()

                }) {

                    ForEach(41..<80) { index in

                        HStack {

                            Text("Row #\(index)")

                            Spacer()

                            

                        }

                    }

                }

            }

            .listStyle(.plain) // PlainListStyle() on SwiftUI 1 and 2

        }

    }

}





#Preview {

    ContentView()

}




No comments:

Create a list in SwiftUI with sticky section headers

 Sample Code import SwiftUI struct ContentView : View {     @State var isCustomViewControllerDisplayed = false     @State private va...