1

I have a UICollectionView and I'm trying to design my UICollectionViewCell's using SwiftUI. I've seen two examples, one and two that have done this in the following manner in their UICollectionViewCell:

let controller = UIHostingController(rootView: SomeSwiftUIView)
let view = controller.view!
view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)
NSLayoutConstraint.activate([
    view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
    view.topAnchor.constraint(equalTo: contentView.topAnchor),
    view.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
    view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])

This works fine visually, but a problem I'm having is I do not have user interaction with the SwiftUI view. For example, if I have a Button in the SwiftUI view, the action is not performed when you tap the button. If a List is in the SwiftUI view, you cannot scroll this view. (I'm not trying to put a List in it, this is just an example)

Any recommendations on where to go from here? Thanks!

Hunter Meyer
  • 314
  • 1
  • 10

1 Answers1

1

I solved this by constraining the view to the cell rather than the contentView, and it is working fine now.

NSLayoutConstraint.activate([
    view.leadingAnchor.constraint(equalTo: leadingAnchor),
    view.topAnchor.constraint(equalTo: topAnchor),
    view.trailingAnchor.constraint(equalTo: trailingAnchor),
    view.bottomAnchor.constraint(equalTo: bottomAnchor)
])

As I suspected, this was a layering issue.

Hunter Meyer
  • 314
  • 1
  • 10