Skip to content

Instantly share code, notes, and snippets.

@karigrooms
Last active February 23, 2021 19:14
Show Gist options
  • Save karigrooms/a40ee6b0be856c1314d11e49340cdd6b to your computer and use it in GitHub Desktop.
Save karigrooms/a40ee6b0be856c1314d11e49340cdd6b to your computer and use it in GitHub Desktop.
Blog post: SwiftUI with UICollectionView - 4. UIHostingController Setup/Cleanup
import SwiftUI
import UIKit
class MyCollectionViewCell: UICollectionViewCell {
lazy var host: UIHostingController = {
return UIHostingController(rootView: Card())
}()
//...
// MARK: UIHostingController Setup/Cleanup
func embed(in parent: UIViewController) {
parent.addChild(host)
host.didMove(toParent: parent)
print("MyCollectionViewCell has been added to parent UIViewController")
}
deinit {
host.willMove(toParent: nil)
host.view.removeFromSuperview()
host.removeFromParent()
print("MyCollectionViewCell has been cleaned up")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment