Skip to content

Instantly share code, notes, and snippets.

@karigrooms
Created February 17, 2021 20:50
Show Gist options
  • Save karigrooms/891497a398baad93d5c072aea8d6fe68 to your computer and use it in GitHub Desktop.
Save karigrooms/891497a398baad93d5c072aea8d6fe68 to your computer and use it in GitHub Desktop.
SwiftUI UIHostingController example for Lessons in SwiftUI blog post
import SwiftUI
import UIKit
class MyViewController: UIViewController {
// this is how you embed a SwiftUI View
lazy var host: UIViewController = {
return UIHostingController(rootView: MySwiftUIView())
}()
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
private func setupView() {
addChild(host)
view.addSubview(host.view)
host.didMove(toParent: self)
host.view.frame = view.frame
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment