首页 > 解决方案 > 如何在 SwiftUI 中使用 Top(行标题/第一行)和 Side(第一列)创建固定视图

问题描述

我正在尝试创建一个固定第一行和第一列的视图。我可以使用固定第一行LazyVStack(alignment: .center, spacing: 10, pinnedViews: [.sectionHeaders])或使用固定第一列LazyHStack(alignment: .center, spacing: 10, pinnedViews: [.sectionHeaders])但不能同时使用两者来创建视图。知道如何在 SwiftUI 中执行此操作。我在网上查看,但找不到使用 SwiftUI 的解决方案。

这是我目前拥有的第一列

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)
    }
}

标签: iosswiftswiftui

解决方案


推荐阅读