UICollectionView和SwiftUI? [英] UICollectionView and SwiftUI?

查看:692
本文介绍了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)
    }
}

列表仍然具有UITableView样式:

List still has UITableView style:

推荐答案

可能的解决方案之一是将UICollectionView包装到UIViewRepresentable中.请参见组合和创建视图 SwiftUI教程,其中,<以c2>为例.

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(测试版)以来,我们可以使用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天全站免登陆