-1

Because SwiftUI has no UICollectionView-like controls,so I used UIKit instead.

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.

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

BaQiWL
  • 417
  • 5
  • 14

1 Answers1

0

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
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • I have tried this solution and it works fine, except I'm unable to interact with the SwiftUI view. I can't press a Button in my SwiftUI view, so I tried adding a List and I'm unable to scroll the List either. Perhaps a layering issue? – Hunter Meyer Nov 17 '20 at 01:12