I could not run your code, I miss JGProgressHUD.
But I noticed your code is not exactly the same I proposed:
you have:
override func viewDidLoad() {
super.viewDidLoad()
setNavigationBar()
let canvasView = PKCanvasView(frame: view.bounds)
self.canvasView = canvasView
canvasView.allowsFingerDrawing = true
view.addSubview(canvasView)
if let sketch = sketch {
SketchService.downloadData(from: sketch.drawingUrl!) { (data) in
self.canvasView.drawing = try! PKDrawing(data: data)
canvasView.delegate = self
self.saveButton.isEnabled = false
}
}
canvasView.delegate = self
saveButton.isEnabled = false
canvasView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(canvasView)
canvasView.backgroundColor = .offWhite
setupToolPicker()
}
and
func setupToolPicker() {
if let window = self.parent?.view.window,
let toolPicker = PKToolPicker.shared(for: window) {
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
canvasView.becomeFirstResponder()
toolPicker.addObserver(self)
updateTools(toolPicker)
}
}
In my case:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated) // Manquait
setTempData()
if #available(iOS 13.0, *) {
let canvasView = PKCanvasView(frame: self.view.bounds)
guard
let window = view.window,
let toolPicker = PKToolPicker.shared(for: window) else { return }
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
canvasView.becomeFirstResponder()
view.addSubview(canvasView)
} else {
// Fallback on earlier versions
}
}
So, your window is:
self.parent?.view.window
in my case:
view.window
Why calling parent ?
Are you sure it is not nil ?