You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中LazyVGrid定义双GridItem却仅显示单网格的问题

问题解决方法

你的代码有两个关键问题导致无法显示双列布局:

  1. 未使用自定义的双列配置
    你已经定义了包含两个GridItem的columns数组,但LazyVGrid的columns参数用的是默认的单列固定宽度项[GridItem(.fixed(200))],根本没用到你写的双列配置。

  2. 错误嵌套ScrollView
    你把ScrollView放在了LazyVGrid的内容里,这会让整个滚动视图被当成一个单独的网格项,自然只能显示单列。而且LazyVGrid本身就支持滚动,完全不需要额外嵌套ScrollView。

修正后的代码如下:

let columns: [GridItem] = [
    GridItem(.flexible(), spacing: nil, alignment: nil),
    GridItem(.flexible(), spacing: nil, alignment: nil),
]

var body: some View {
    LazyVGrid(columns: columns) {
        ForEach(0..<20) { index in
            Rectangle()
                .frame(height: 150)
        }
    }
}

这样修改后,就能正常显示双列的Rectangle布局了。

内容的提问来源于stack exchange,提问作者Jagadish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 07:15:56