import SwiftUI
struct UIKitAppear: UIViewControllerRepresentable {
let action: () - Void
func makeUIViewController(context: Context) - UIAppearViewController {
let vc = UIAppearViewController()
vc.delegate = context.coordinator
return vc
}
func updateUIViewController(_ controller: UIAppearViewController, context: Context) {}
func makeCoordinator() - Coordinator {
Coordinator(action: self.action)
}
class Coordinator: ActionRepresentable {
var action: () - Void
init(action: @escaping () - Void) {
self.action = action
}
func remoteAction() {
action()
}
}
}
protocol ActionRepresentable: AnyObject {
func remoteAction()
}
class UIAppearViewController: UIViewController {
weak var delegate: ActionRepresentable?
override func viewDidLoad() {
view.addSubview(UILabel())
}
override func viewDidAppear(_ animated: Bool) {
delegate?.remoteAction()
}
}
public extension View {
func onUIKitAppear(_ perform: @escaping () - Void) - some View {
self.background(UIKitAppear(action: perform))
}
}