2

我想创建一个属性列表(不使用列表视图)。每个属性都是一个 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 来完成我上面描述的相对宽度。我究竟做错了什么?

4

1 回答 1

1

这是可能解决方案的演示。使用 Xcode 11.4 / iOS 13.4 测试

演示

注意:ViewHeightKey取自另一个我的解决方案

struct ChildView: View {
    let attribute: Attribute

    @State private var fitHeight = CGFloat.zero

    var body: some View {
        GeometryReader { geometry in
            HStack(alignment: .top, spacing: 0) {
                Text(self.attribute.name)
                    .bold()
                    .frame(width: 0.3 * geometry.size.width, alignment: .leading)
                    .background(Color.yellow)
                Text(self.attribute.value)
                    .fixedSize(horizontal: false, vertical: true)
                    .frame(width: 0.7 * geometry.size.width, alignment: .leading)
            }
            .background(Color.red)
            .background(GeometryReader {
                Color.clear.preference(key: ViewHeightKey.self,
                    value: $0.frame(in: .local).size.height) })
        }
        .onPreferenceChange(ViewHeightKey.self) { self.fitHeight = $0 }
        .frame(height: fitHeight)
    }
}
于 2020-07-20T10:17:12.790 回答