UICollectionView 和 SwiftUI? [英] UICollectionView and SwiftUI?

查看:37
本文介绍了UICollectionView 和 SwiftUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 SwiftUI 创建方形项目的网格(例如在 iOS 照片库中)?

How to create grid of square items (for example like in iOS Photo Library) with SwiftUI?

我尝试过这种方法,但它不起作用:

I tried this approach but it doesn't work:

var body: some View {
    List(cellModels) { _ in
        Color.orange.frame(width: 100, height: 100)
    }
}

List 仍然具有 UITableView 样式:

List still has UITableView style:

推荐答案

可能的解决方案之一是将您的 UICollectionView 包装到 UIViewRepresentable 中.请参阅 组合和创建视图 SwiftUI 教程,其中包含 MKMapView 为例.

One of the possible solutions is to wrap your UICollectionView into UIViewRepresentable. See Combining and Creating Views SwiftUI Tutorial, where they wrap the MKMapView as an example.

到目前为止,SwiftUI 中还没有与 UICollectionView 等效的功能,而且还没有相关计划.请参阅该 tweet 下的讨论.

By now there isn’t an equivalent of UICollectionView in the SwiftUI and there’s no plan for it yet. See a discussion under that tweet.

要获取更多详细信息,请查看集成 SwiftUI WWDC 视频(~8:08).

To get more details check the Integrating SwiftUI WWDC video (~8:08).

更新:

从 iOS 14 (beta) 开始,我们可以使用 Lazy*Stack 来至少实现 SwiftUI 中集合视图的性能.说到单元格的布局,我认为我们仍然需要在每行/每列的基础上手动管理它.

Since iOS 14 (beta) we can use Lazy*Stack to at least achieve the performance of the collection view in the SwiftUI. When it comes to the layout of cells I think we still have to manage it manually on a per-row/per-column basis.

这篇关于UICollectionView 和 SwiftUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆