0

I'm using a 3rd party library developed with UIKit. It's API needs a reference to a UIView.

How can I use this library inside SwiftUI? And how can I convert a SwiftUI view to a UIView?

I've tried creating a UIViewRepresentable like this:

struct SomeView: UIViewRepresentable {

    let contentViewController: UIViewController

    init<Content: View>(contentView: Content) {
        self.contentViewController = UIHostingController(rootView: contentView)
    }

    func makeUIView(context: Context) -> UIKitView {

        // Whatever I do here doesn't work. Frame is always 0

        contentViewController.loadViewIfNeeded() 
        contentViewController.view.setNeedsDisplay()
        contentViewController.view.layoutIfNeeded()

        print(contentViewController.view.frame)

        let uikitView = UIKitView()
        uikitView.show(contentViewController.view)
        return popover
    }

    func updateUIView(_ uiView: UIKitView, context: Context) {
    }
}
Peter Warbo
  • 11,136
  • 14
  • 98
  • 193
  • You can see for example [here](https://stackoverflow.com/a/59853272/12299030) the simple case of construction `UIViewRepresentable` and its usage in post question use-case. – Asperi Jan 31 '20 at 15:39
  • @Asperi i don't think you read my question in entirety – Peter Warbo Jan 31 '20 at 17:13

0 Answers0