【问题标题】:Custom grid in swiftswift中的自定义网格
【发布时间】:2021-12-26 20:03:41
【问题描述】:

我需要一些帮助来构建这个 UI。我想要的是制作一个这个网格1

到目前为止,我尝试过的是这样,但是 Sets、reps、rest 之间的填充。

struct ProgramContainer: View {
var weight: String = "Weight"
@State var value = ""

private var columns: [GridItem] = [
    GridItem(.flexible(minimum: 0, maximum: 40), spacing: 40),
    GridItem(.flexible(minimum: 1, maximum: 40), spacing: 40),
    GridItem(.flexible(minimum: 1, maximum: 40), spacing: 40),
    GridItem(.flexible(minimum: 1, maximum: 40), spacing: 40)
]

var body: some View {
    ScrollView {
        VStack(alignment: .leading) {
            Text("name")
                .font(.title3).bold()
            Divider()
            LazyVGrid(
                columns: columns,
                alignment: .leading,
                spacing: 20,
                pinnedViews: [.sectionHeaders, .sectionFooters]
            ) {
                Section {
                    ForEach(0...11, id: \.self) {index in
                        TextField("0", text: $value)
                    }
                } header: {
                    HStack(spacing: 40, content: {
                        Text(weight)
                        Text("Reps")
                        Text("Sets")
                        Text("Rest")
                    })
                }
            }
        }
    }
}

}

它看起来像这样:

【问题讨论】:

  • 怎么可能是使用 LazyVGrid 卧底的自定义网格?!自定义意味着您应该从零开始构建 Grid。但是您的代码在项目中显示了 LazyVGrid 的用例!
  • 我虽然你可以用它来构建一个自定义网格....
  • 欢迎来到 Stack Overflow!请拨打tour 并查看:How do I ask a good question?。你没有问过问题。你希望你的网格是什么样子的?
  • 感谢您的评论,但我已经附上了一张我的网格应该是什么样子的图片。
  • @FrederikHjorth - 这可能会有所帮助:stackoverflow.com/questions/69853681/…

标签: ios swift swiftui


【解决方案1】:

我发现如果您使用具有相同列数 (GridItems) 的 LazyVGrid 作为 Section 标题,则更容易对齐标题和项目。

锻炼项目:

struct Workout: Identifiable {
let id = UUID()
let weight: Int
let sets: Int
let reps: Int
let rest: Int

}

页眉视图:

struct WorkoutHeaderView: View {
private let labels = ["Weight", "Set", "Reps", "Rest"]
private let gridItems: [GridItem] = [GridItem(), GridItem(), GridItem(), GridItem()]

var body: some View {
    LazyVGrid(columns: gridItems, spacing: 10) {
        ForEach(labels, id: \.self) { label in
            Text(label)
        }
    }
}

}

程序容器:

struct ProgramContainer: View {
@State private var workoutData: [Workout] = [
    .init(weight: 100, sets: 3, reps: 12, rest: 1),
    .init(weight: 120, sets: 3, reps: 10, rest: 2)
]
private let gridItems: [GridItem] = [GridItem(), GridItem(), GridItem(), GridItem()]

var body: some View {
    VStack(spacing: 20) {
        VStack(alignment: .leading) {
            Text("Label")
            Divider()
        }
        
        LazyVGrid(columns: gridItems, spacing: 10) {
            Section(header: WorkoutHeaderView()) {
                ForEach(workoutData) { value in
                    Text("\(value.weight)")
                    Text("\(value.sets)")
                    Text("\(value.reps)")
                    Text("\(value.rest)")
                }
            }
        }
        
        Spacer()
            .frame(maxHeight: 150)
    }
    .padding()
    .background(
        RoundedRectangle(cornerRadius: 20)
            .foregroundColor(.blue)
            .opacity(0.5)
    )
    .padding()
}

}

Layout Result

【讨论】:

  • 非常感谢您的回复对我帮助很大,您知道是否可以使用文本字段制作网格视图,您可以在其中逐个编辑它们?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 2015-09-06
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多