我想创建一个属性列表(不使用列表视图)。每个属性都是一个 HStack,其中包含两个文本,名称和值。我希望名称文本始终具有整个 HStack 宽度的 30%,而值文本则使用其余的水平空间。每个属性的高度取决于内容。
我尝试通过以下观点来完成它:
struct FatherList: View {
let attributes: Attributes
init(_ attributes: Attributes) {
self.attributes = attributes
}
var body: some View {
VStack(spacing: CGFloat.spacing.medium) {
ForEach(
attributes,
id: \.name,
content: ChildView.init
)
}
}
}
其中包含以下 ChildView:
struct ChildView: View {
let listItem: Product.Attribute
init(_ attribute: Product.Attribute) {
self.attribute = attribute
}
var body: some View {
GeometryReader { geometry in
HStack(alignment: .top, spacing: 0) {
Text(attribute.name)
.bold()
.frame(width: 0.3 * geometry.size.width)
.background(Color.yellow)
Text(attribute.value)
}
.fixedSize(horizontal: false, vertical: true)
.background(Color.red)
}
}
}
我得到的结果是这样的:
子视图重叠,这不是我想要的,我希望子视图扩展并相互跟随。我正在使用 geometryReader 来完成我上面描述的相对宽度。我究竟做错了什么?