Last active
February 23, 2021 19:14
-
-
Save karigrooms/a40ee6b0be856c1314d11e49340cdd6b to your computer and use it in GitHub Desktop.
Blog post: SwiftUI with UICollectionView - 4. UIHostingController Setup/Cleanup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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