UICollectionView和SwiftUI? [英] UICollectionView and 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屋!