因为SwiftUI
没有UICollectionView-like
控件,所以我改用了UIKit
。
根据interfacing-with-uikit,我知道SwiftUI
如何与UIKit
交互,但是我想进一步使用SwiftUI
封装UICollectionViewCell
,并使用SwiftUI
填充UICollectionViewCell
的内容,我可以这样做吗。
据我所知SwiftUI
中的列表是根据UITableView
封装的,我希望SwiftUI
与UICollectionView
相同,我不想使用某些三方库来做到这一点,我想学习秘密
假设您具有CellContentView
SwiftUI视图,则可以使用类似下面的方法将其集成到UICollectionViewCell
中
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
}