ScrollView 内的 VStack 内的 LazyVGrid 以及其他元素导致崩溃

LazyVGrid inside a VStack inside a ScrollView among other elements causes a crash

这是我的视图代码:

ScrollView {
    VStack {
        someView

        LazyVGrid(
            columns: [
                GridItem(
                    .adaptive(minimum: metrics.size.width / 2),
                    spacing: 0
                )
            ],
            spacing: 0
        ) {
            ForEach(items, id: \.self) { item in
                getItemView(item)
            }
        }

        someOtherView
    }
}

这会导致崩溃:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
#0  0x000000011242c8d6 in specialized HVGrid.minorGeometry(updatingSize:) ()
#1  0x00000001125464fe in specialized IncrementalStack<>.sizeThatFits(proposedSize:children:context:state:) ()
#2  0x000000011254bf61 in protocol witness for IncrementalLayout.sizeThatFits(proposedSize:children:context:state:) in conformance LazyHGridLayout ()
#3  0x000000011254bee7 in protocol witness for IncrementalLayout.sizeThatFits(proposedSize:children:context:state:) in conformance LazyVGridLayout ()
#4  0x0000000112591a63 in closure #1 in IncrementalLayoutComputer.Engine.sizeThatFits(_:) ()
#5  0x0000000112591897 in IncrementalLayoutComputer.Engine.sizeThatFits(_:) ()
#6  0x0000000112bc2ec0 in LayoutComputer.EngineDelegate.sizeThatFits(_:) ()
#7  0x0000000112700ddf in _PaddingLayout.sizeThatFits(in:context:child:) ()
#8  0x00000001127012d1 in protocol witness for UnaryLayout.sizeThatFits(in:context:child:) in conformance _PaddingLayout ()
#9  0x0000000112709c95 in closure #1 in UnaryLayoutEngine.sizeThatFits(_:) ()
#10 0x0000000112709b61 in UnaryLayoutEngine.sizeThatFits(_:) ()

我该如何解决这个问题?

这里有 2 种方法可以解决此问题。要么

  1. 使用 LazyVStack 而不是 VStack

  2. 或明确指定 LazyVGrid 的宽度:

    LazyVGrid(
        columns: [
            GridItem(
                .adaptive(minimum: metrics.size.width / 2),
                 spacing: 0
            )
        ],
        spacing: 0
    ) {
        ForEach(items, id: \.self) { item in
            getItemView(item)
        }
    }
    // add this
    .frame(width: metrics.size.width)