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

如何点击LazyVGrid网格项跳转至新视图(类似UICollectionView)

让SwiftUI LazyVGrid中的网格项整体可点击跳转(iOS16+)

直接用NavigationLink把整个ContactCell包裹起来就行,适配iOS16+的NavigationStack,完美替代被弃用的isActive方案,整个网格项区域都会变成可点击的跳转入口,不需要额外加文本链接。

修改后的代码如下:

var body: some View {
    
    NavigationStack {
        VStack {
            // Contacts Scroll View
            ScrollView {
                LazyVGrid(columns: threeColumnGrid, spacing: 20) {
                    ForEach($contacts, id: \.self) { $contact in
                        // 用NavigationLink包裹ContactCell,实现整体可点击跳转
                        NavigationLink(destination: ContactDetailView(contact: contact)) {
                            ContactCell(firstName: $contact.firstName.wrappedValue)
                        }
                        // 移除默认的按钮样式,保持Cell原有外观
                        .buttonStyle(.plain)
                    }
                }.padding(EdgeInsets(top: 20,
                                     leading: 20,
                                     bottom: 20,
                                     trailing: 20))
            }
        }.background(Color(CustomColors.background.rawValue))
    }
}

关键说明:

  • 把你实际要跳转的目标页面替换掉代码里的ContactDetailView,根据需要传递contact数据
  • .buttonStyle(.plain)是可选但推荐的设置,它会去掉NavigationLink默认的高亮、边框等样式,让Cell点击时和原本的视觉效果一致
  • 这种方案完全符合iOS16+的SwiftUI导航规范,不用再纠结被弃用的isActive属性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 19:05:25