Skip to content

Instantly share code, notes, and snippets.

@sahara-ooga
Created August 8, 2021 14:26
Show Gist options
  • Save sahara-ooga/b9ee5078f8c0f9253ad2656e62d3bbee to your computer and use it in GitHub Desktop.
Save sahara-ooga/b9ee5078f8c0f9253ad2656e62d3bbee to your computer and use it in GitHub Desktop.
SwiftUI's View in UIKit
import UIKit
import SwiftUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configure(BindingView())
}
}
extension ViewController {
func configure<T:View>(_ swiftUIView: T) {
let childView = UIHostingController(rootView: swiftUIView)
addChild(childView)
childView.view.frame = view.frame
view.addSubview(childView.view)
childView.didMove(toParent: self)
}
}
struct BindingView: View {
@State var inputText = ""
var body: some View {
VStack {
TextField("", text: $inputText)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
Text(inputText)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment