Created
August 8, 2021 14:26
-
-
Save sahara-ooga/b9ee5078f8c0f9253ad2656e62d3bbee to your computer and use it in GitHub Desktop.
SwiftUI's View in UIKit
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 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