Protect A Pencil Drawing When iPhone Duo Changes Layout
A sketch should not become a new document because its toolbar moved. Yet a drawing app can accidentally do exactly that when its compact and expanded layouts each create their own canvas.
The iPhone Duo announcement makes this worth checking now. Apple announced the device on September 9, with availability beginning October 23. Its announcement says Apple Pencil with USB-C support arrives later this year. That is a separate promise from the hardware’s first shipping date. This article prepares the drawing architecture; it does not assume Pencil input is already available on Duo.
Keep one canvas alive for one editing session
PencilKit’s PKCanvasView captures strokes and maintains the interactive drawing surface. Replacing that view during an ordinary resize risks losing more than the visible picture: selection, the current interaction, viewport state, and undo behavior all deserve attention.
For a single editor, a small owner can retain its canvas. The following complete bridge creates the drawing once and deliberately does not reassign it in updateUIView:
import PencilKit
import SwiftUI
@MainActor
final class DrawingSession: NSObject, ObservableObject, PKCanvasViewDelegate {
let canvas: PKCanvasView
@Published private(set) var revision = 0
init(drawing: PKDrawing = PKDrawing()) {
canvas = PKCanvasView()
super.init()
canvas.drawing = drawing
canvas.contentSize = CGSize(width: 1600, height: 1200)
canvas.drawingPolicy = .anyInput
canvas.tool = PKInkingTool(.pen, color: .label, width: 4)
canvas.delegate = self
}
func canvasViewDrawingDidChange(_ canvasView: PKCanvasView) {
revision += 1
}
func snapshot() -> (revision: Int, data: Data) {
(revision, canvas.drawing.dataRepresentation())
}
}
struct RetainedCanvas: UIViewRepresentable {
let session: DrawingSession
func makeUIView(context: Context) -> PKCanvasView { session.canvas }
func updateUIView(_ view: PKCanvasView, context: Context) {
// Layout never replaces the current drawing.
}
}
struct DrawingEditor: View {
@StateObject private var session = DrawingSession()
var body: some View {
RetainedCanvas(session: session)
.overlay(alignment: .bottomTrailing) {
Text("Revision \(session.revision)")
.padding()
}
}
}
DrawingEditor owns the session through StateObject. Keep that editor at a stable identity in your document screen. Do not give it an ID based on width or fold state, and do not build separate editor instances inside compact and expanded branches.
This owner belongs to one editor. A UIKit view cannot appear in two view hierarchies simultaneously. If two scenes open the same document, give them separate canvases and coordinate document changes through your document layer. Share the durable drawing, not the same PKCanvasView instance.
The example uses a fixed pen and accepts finger input to make the baseline useful on existing hardware. Tool-picker presentation and Pencil-specific interactions are separate integrations. Do not make the only way to create or edit content depend on an accessory whose availability you have not checked.
Resize the viewport, not the stored artwork
The example’s 1600-by-1200 content size is an illustrative document coordinate system. It is independent of the canvas frame. A narrow window shows a smaller viewport into that document; it does not redefine what a stroke’s location means.
Avoid applying a permanent transform to every stroke each time the window changes size. Repeatedly scaling the document to fit the current frame turns a presentation change into an edit and can accumulate surprising changes to geometry.
If the product should fit the drawing on first open, calculate a viewport zoom for that action. Once the person pans or zooms, preserve their chosen location through subsequent layout changes when possible. Reapply automatic fitting only for an explicit Fit command or a clearly defined document transition.
Toolbar placement, safe areas, and display regions still matter. The Duo layout preparation article covers those boundaries. Here the invariant is narrower: changing the surrounding layout must not replace the drawing session or rewrite its strokes.
Save editable data before making a preview
A PNG is useful for the document browser, but it is not an editable PencilKit document. PKDrawing supports a data representation and reconstruction with init(data:).
The owner’s snapshot() captures a revision and bytes together on the main actor. Pass that immutable snapshot to your persistence worker. Track the saved revision only after the write succeeds; a failed write must leave the document dirty.
Do not encode the entire drawing on every body evaluation or every geometry update. A save debounce can coalesce drawing changes, while explicit close/background handling can request the latest snapshot. Keep ordering in the writer so a slower old save cannot replace a newer one. The revision-ordered save approach discusses that problem separately.
Loading another document is also explicit. Construct its session from decoded drawing data and use the document’s stable identity. Do not silently reload a disk copy into a live canvas just because SwiftUI called updateUIView again.
Check the transition while an edit is interesting
Start with several strokes, erase part of one, undo, zoom in, and pan away from the origin. Resize the window and move the toolbar. Confirm that the drawing has the same strokes, the viewport remains sensible, and undo still corresponds to the editing session.
Then save, close the document, and reopen it from PencilKit data. Compare editable content, not just a screenshot. Exercise a save failure and a document switch while a save is pending. Those cases reveal ownership mistakes much faster than an empty canvas does.
September 2026 verification: the complete owner and SwiftUI bridge type-checked for iOS 17 using SDK 27.0. PencilKit interaction, undo during live resize, and Duo hardware were not exercised. Duo-specific layout APIs require their own SDK verification; this baseline does not call them.