0

I'm building an app using mostly SwiftUI, but I have to use a UIKit ViewController to interface with PencilKit. I'm using UIViewControllerRepresentable to interface between them.

So I have a ViewController which I'm using to set up the PencilKit part of my app, and inside it I have to set a PKToolPicker which depends on a UIResponder. To do so I have to write the following code:

canvasView = PKCanvasView(frame: view.bounds)
toolPicker.setVisible(true, forFirstResponder: canvasView)
canvasView.becomeFirstResponder()

This canvasView view I use here is the view which controls the drawing area, and you usually have to set it as a subview of the aforementioned ViewController. The issue is that in my case, this ViewController is nested inside a UIViewControllerRepresentable which is nested inside a SwiftUI View. So, I need to add this SwiftUI view as a second FirstResponder or else the PKTool disappears each time I run a gesture on an SwiftUI view, as canvasView stops being first responder.

So my question is how do I do this? How can I set up a SwiftUI view as a first responder? Couldn't find how to obtain a SwiftUI's UIResponder object though so I could add it as a FirstResponder, anyone know how to?

Hamster
  • 133
  • 3
  • 12

1 Answers1

0

I'm not really clear about your particular problem, but I assumed that in your UIViewControllerRepresentable you have also

canvasView.becomeFirstResponder()

just before your

toolPicker.setVisible(true, forFirstResponder: canvasView)
  • Hey, thanks for the answer! Just re read my question and realized its quite a distaster. Re wrote most of it to make it clearer. And yes you are right, I have to execute `becomeFirstResponder` on it, added it to the question. – Hamster Mar 25 '20 at 00:16