This is one of the ways to solve it.
We solved it in a week session called Saturday Night Coder.
Change Development Info
Maybe we set the setting iOS 14.0. Please change it into iOS 13.0.
And write a code
import UIKit
// Pencilkit
import PencilKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let canvas = PKCanvasView(frame: view.frame)
view.addSubview(canvas)
canvas.tool = PKInkingTool(.pen, color: .black, width: 30)
// PKToolPicker
if let window = UIApplication.shared.windows.first {
if let toolPicker = PKToolPicker.shared(for: window) {
toolPicker.addObserver(canvas)
toolPicker.setVisible(true, forFirstResponder: canvas)
canvas.becomeFirstResponder()
}
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let canvasView = PKCanvasView(frame: view.frame)
view.addSubview(canvasView)
guard
let window = view.window,
let toolPicker = PKToolPicker.shared(for: window) else { return }
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
canvasView.becomeFirstResponder()
}
}