【发布时间】:2020-01-31 18:48:21
【问题描述】:
我不明白为什么在使用 GeometryReader 呈现自定义水平分隔线时,VStack 内的 VStack 内的元素之间存在无关的垂直间距。 /p>
ScrollView {
ForEach(self.model1.elements, id: \.self) { element in
VStack.init(alignment: .leading, spacing: 0) {
// Text("test") // image 3: works correctly
// .background(Color.blue)
GeometryReader { geometry in
Path { path in
path.move(to: .init(x: 0, y: 0))
path.addLine(to: .init(x: geometry.size.width, y: 0))
}
.strokedPath(.init(lineWidth: 1, dash: [1,2]))
}
// .frame(maxHeight: 1) // image 2: uncommenting this line doesn't fix the spacing
.foregroundColor(.red)
.background(Color.blue)
HStack {
Text("\(element.index+1)")
.font(.system(.caption, design: .rounded))
.frame(width: 32, alignment: .trailing)
Text(element.element)
.font(.system(.caption, design: .monospaced))
.frame(maxWidth:nil)
Spacer()
}
.frame(maxWidth:nil)
.background(Color.green)
}
.border(Color.red)
}
}
.frame(maxHeight: 1) 的蓝色填充消失了,但在连续的 HStacks 之间仍有空白。
我希望垂直间距与此图像中的一样,即 0。此图像是通过取消注释 Text("test") 源代码并注释 GeometryReader 代码来实现的。
我正在使用 Xcode 11.3.1 (11C504)
【问题讨论】:
标签: swiftui