Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaisukeNagata/a4c09e9e87dbcb7b27a9c98b8166e787 to your computer and use it in GitHub Desktop.
Save DaisukeNagata/a4c09e9e87dbcb7b27a9c98b8166e787 to your computer and use it in GitHub Desktop.
RepresentableViewController
import SwiftUI
struct ContentView: View {
var body: some View {
RepresentableViewController(view: HelloWorldColorView())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct HelloWorldColorView: View {
var body: some View {
ZStack {
Text("Hello, World!")
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.green)
.edgesIgnoringSafeArea(.all)
}
}
struct RepresentableViewController: UIViewControllerRepresentable {
@State var view: HelloWorldColorView
@State private var viewController = UIViewController()
func makeUIViewController(context: UIViewControllerRepresentableContext<RepresentableViewController>) -> UIViewController {
DispatchQueue.main.async {
let view = UIHostingController(rootView: self.view).view ?? UIView()
view.frame = UIScreen.main.bounds
self.viewController.view.addSubview(view)
}
return viewController
}
func makeCoordinator() -> Coordintor { return Coordintor(owner: self) }
func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<RepresentableViewController>) {}
final class Coordintor: NSObject, UIDocumentInteractionControllerDelegate {
let owner: RepresentableViewController
init(owner: RepresentableViewController) {
self.owner = owner
}
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return owner.viewController
}
func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
controller.delegate = nil
}
}
}
@DaisukeNagata
Copy link
Author

スクリーンショット 2020-08-13 21 38 34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment