2

I am trying to create a view with the first row and the first column pinned. I am able to create a view with either the first row pinned using LazyVStack(alignment: .center, spacing: 10, pinnedViews: [.sectionHeaders]) or pin the first column using LazyHStack(alignment: .center, spacing: 10, pinnedViews: [.sectionHeaders]) but not both. Any idea how to do this in SwiftUI. I looked online but could not find a solution with SwiftUI.

Here is what I have currently that pins the first column

struct ContentView: View {
    private var colors: [Color] = [.blue, .yellow, .green]
    private var gridItems = Array(repeating: GridItem(.fixed(50)), count: 10)
    var body: some View {
        ScrollView(.horizontal) {
            LazyHGrid(rows: gridItems, spacing: 5, pinnedViews: [.sectionHeaders]) {
                Section(header: headerView(1)) {
                    ForEach((0 ..< 10), id:\.self) { idx in
                        ForEach((0..<10), id: \.self) { index in
                            CellContent(rowIndex: idx, colIndex: index,
                                        color: colors[index % colors.count])
                        }
                    }
                }
            }.frame(height: 570)
            .padding(5)
        }
    }
    
    private func headerView(_ index: Int) -> some View{
        Text("Section \(index)")
            .padding()
            .foregroundColor(Color.white)
            .font(.title)
            .frame(maxHeight: .infinity)
            .background(Color.blue)
    }
}
RXP
  • 517
  • 4
  • 18
  • This works https://stackoverflow.com/questions/75395335/scrollview-preferencekey-offset-changes-from-ios14-ios16?noredirect=1&lq=1 but it breaks on iOS16. Did you solve it? – app4g Feb 12 '23 at 08:19

0 Answers0