This package should have been named
SwiftUIKitView
, turns out it already exists 🤓
This package allows you to directly insert SwiftUI components and syntax inside your UIKit legacy code.
final class MyViewController: UIViewController {
// this is UIKit
override func viewDidLoad() {
super.viewDidLoad()
let button = UIKitSwiftView {
// and this is SwiftUI 🎉
Button {
print("some action")
} label: {
Text("This is a SwiftUI Button")
}
}
view.addSubview(button)
}
}
The UIKitSwiftView
can also observe some ObservableObject
.
final class MyModel: ObservableObject {
@Published var title: String = "Some title"
}
let model = MyModel()
let view = UIKitSwiftView(observing: model) {
Text(model.title)
}