-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

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.