Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Behavior of Drawings in Portrait/Landscape Mode

The app provides a simple drawing functionality on a canvas without using a storyboard. After creating drawings in portrait mode, rotating the simulator or the iPad-Device to landscape mode does not update or rescale the existing drawings to fit the newly expanded canvas area. The app can also be launched directly in landscape mode, allowing drawings to be created before rotating the device back to portrait mode. However, the issue persists, with the drawings not adjusting to the new canvas dimensions.

The goal is to achieve the same behavior as in the Apple Notes app: when the iPad is rotated, the drawings should adjust to the newly expanded or reduced canvas area. I would appreciate any suggestions or solutions.

Sample Projekt [https://github.com/GG88IOS/Test_Drawing_App)

Answered by Claude31 in 837800022

I tested your code. When rotating, the canvas size changes, but the drawing remains the same.

I cannot find immediately in your code what should make the existing drawing scale to the new format. Where is it ?

Is it here ?

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        guard let context: CGContext = UIGraphicsGetCurrentContext() else { return }
        for stroke in Database.strokes {
            context.setLineCap(.round)
            context.setLineJoin(.round)
            context.setLineWidth(2.0)
            context.setAlpha(1.0)
            context.setBlendMode(.normal)
            context.setStrokeColor(UIColor.black.cgColor)
            context.addPath(stroke.path)
            context.strokePath()
        }
    }

But Database.strokes is not scaled on device rotation. Why should the drawing change ?

Try to scale Database.strokes when you rotate device, then drawing will adapt.

Accepted Answer

I tested your code. When rotating, the canvas size changes, but the drawing remains the same.

I cannot find immediately in your code what should make the existing drawing scale to the new format. Where is it ?

Is it here ?

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        guard let context: CGContext = UIGraphicsGetCurrentContext() else { return }
        for stroke in Database.strokes {
            context.setLineCap(.round)
            context.setLineJoin(.round)
            context.setLineWidth(2.0)
            context.setAlpha(1.0)
            context.setBlendMode(.normal)
            context.setStrokeColor(UIColor.black.cgColor)
            context.addPath(stroke.path)
            context.strokePath()
        }
    }

But Database.strokes is not scaled on device rotation. Why should the drawing change ?

Try to scale Database.strokes when you rotate device, then drawing will adapt.

First of all, thank you very much for your help. The question I'm asking myself is how to approach this correctly. Should I go through each stroke one by one (with a for_each loop) on every rotation of the iPad and scale them using a transform function?

Thank you Claude31. you gave me the clue to solve the problem.

As you save the stokes, going through eac stroke is probably the easiest ans safest way.

Save the original canvas size.

Don’t change the stores strokes, just adapt to the canvas size.

Behavior of Drawings in Portrait/Landscape Mode
 
 
Q