PencilKit SwiftUI issue (disappearing strokes) on iOS 16 Simulator

We are using a SwiftUI view backed by PencilKit and are experiencing an issue on iOS 16 where drawn strokes disappear from the drawing (the drawing is empty after the stroke ends). Debugging the issue shows that PKCanvasView's drawing collection of strokes is increasing, yet they are not being rendered.

Details:

  • I'm on macOS 12.6 Monterey
  • The bug surfaces in Xcode 14.1 / iOS 16.1 (I didn't check the Xcode 14.0 / 14.0.1).
  • The code works as expected in Xcode 13.4.1 / iOS 15.5.
  • The bug appears not to happen on device (iPhone 13 Pro / iOS 16.1.1)

Example code demonstrating the issue:

I have added a couple of print statements to show what is happening and to count the brush strokes of the PKDrawing. Please see the attached GIFs below that demonstrate the issue.

import SwiftUI
import PencilKit

public struct TestCanvas {
    private static let defaultTool = PKInkingTool(.pen, color: .darkGray, width: 5)
}

// MARK: UIViewRepresentable

extension TestCanvas: UIViewRepresentable {
    public func makeUIView(context: Context) -> some UIView {
        print("make ui view")
        let canvasView = PKCanvasView()
        canvasView.delegate = context.coordinator
        canvasView.drawingPolicy = .anyInput
        canvasView.tool = Self.defaultTool
        canvasView.isRulerActive = false
        canvasView.backgroundColor = .clear
        canvasView.isOpaque = true
        return canvasView
    }

    public func updateUIView(_ uiView: UIViewType, context: Context) {
        print("update ui view")
    }

    public func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
}

// MARK: Coordinator

public extension TestCanvas {
    @MainActor final class Coordinator: NSObject {
        private let parent: TestCanvas

        init(_ parent: TestCanvas) {
            print("init coordinator")
            self.parent = parent
            super.init()
        }
    }
}

// MARK: PKCanvasViewDelegate

extension TestCanvas.Coordinator: PKCanvasViewDelegate {
    public func canvasViewDidEndUsingTool(_ canvasView: PKCanvasView) {
        print("canvas did end using tool (\(canvasView.drawing.strokes.count) strokes)")
    }

    public func canvasViewDrawingDidChange(_ canvasView: PKCanvasView) {
        print("drawing did change (\(canvasView.drawing.strokes.count) strokes)")
    }
}

Xcode 14.1 / iOS 16.1

PencilKit issue occurs on Xcode 14.1 / iOS 16.1 Simulator. Note the strokes disappear, yet the drawing's strokes keep incrementing.

Xcode 13.4.1 / iOS 15.5

It works fine in Xcode 13.4.1 / iOS 15.5 Simulator.

iPhone 13 Pro / iOS 16.1.1

I don't experience the issue on device (iPhone 13 Pro, iOS 16.1.1).

You are not alone in this. PKCanvasView is completely unusable in Xcode 14+. Any drawings just disappear automatically.

I believe this might only affect the simulator. I just exported an IPA using Xcode 14.1 and ran it in BrowserStack. This issue did not happen there, but it's occurring in the Xcode simulator.

Yeah, I only experience the issue when running in iOS 16.1 Simulator. On device it seems to work as expected.

PencilKit SwiftUI issue (disappearing strokes) on iOS 16 Simulator
 
 
Q