如何使用 SwiftUI 在 UICollectionViewCell 中填充内容 [英] How can I use SwiftUI to populate content in a UICollectionViewCell
问题描述
因为 SwiftUI
没有 UICollectionView-like
控件,所以我用 UIKit
代替.
Because SwiftUI
has no UICollectionView-like
controls,so I used UIKit
instead.
根据interifying-with-uikit,我知道如何SwiftUI
与UIKit
交互,但我想进一步使用SwiftUI
封装UICollectionViewCell
,并使用SwiftUI
来填充 UICollectionViewCell
的内容,我该怎么做.
According to interfacing-with-uikit, I know how SwiftUI
interacts with UIKit
, but I want to further use SwiftUI
to encapsulate UICollectionViewCell
, and use SwiftUI
to fill the content of UICollectionViewCell
, how can I do that.
据我所知SwiftUI
中的List是按照UITableView
封装的,我希望SwiftUI
对UICollectionView也做同样的事情
,我不想用一些三方库来做这个,我想学习秘诀
As far as I know List in SwiftUI
are encapsulated according to UITableView
, I want SwiftUI
to do the same with UICollectionView
,I don't want to use some tripartite libraries to do this, I want to learn the secrets
推荐答案
假设您有 CellContentView
SwiftUI 视图,您可以使用类似以下内容将其集成到 UICollectionViewCell
中
Assuming you have CellContentView
SwiftUI view, you can integrate it in UICollectionViewCell
using something like the following
if let cellContent = UIHostingController(rootView: CellContentView()).view {
cell.contentView.addSubview(cellContent)
cellContent.leadingAnchor.constraint(equalTo: cell.leadingAnchor).isActive = true
cellContent.trailingAnchor.constraint(equalTo: cell.trailingAnchor).isActive = true
cellContent.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
cellContent.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true
}
这篇关于如何使用 SwiftUI 在 UICollectionViewCell 中填充内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!