apple developer

home tech-notes github twitter stackoverflow dev.to

Scaling a Scene’s Content to Fit the View

Configure the scale mode to determine how a scene is sized to fit its view.


Set the scene’s scaleMode property to determine how it’s sized according to the view that’s displaying it. For example, in iOS, scaleMode determines the automatic resizing of your scene when the device changes orientation. In macOS, scaleMode affects how your scene is scaled when the user changes the size or proportions of your app’s window.

Decide Which Scale Mode Is Best

When you design your game, you should decide on a strategy for handling the scene’s size and scaleMode properties. Here are the most common strategies:

Set a Fixed Aspect Ratio

Listing 1 shows a typical implementation for when you plan to use a constant-sized scene. This code specifies a method to be executed the first time that the scene is presented. It configures the scene’s properties, including its scaling mode, then adds content. In this example, the scale mode is set to SKSceneScaleMode.aspectFit, which scales the contents equally in both dimensions and ensures that all of the scene’s contents are visible. Where necessary, this mode adds letter-boxing.

Listing 1 Creating a constant-sized scene
func createSceneContent() {
    scene.scaleMode = .aspectFit
    scene.backgroundColor = .black
    // Add additional scene contents here.  

Update Your Graphics on Scene Resize

If you expect a scene’s size to change at runtime, then the initial scene size should be used to determine which art assets to use, as well as any game logic that is dependent on the scene size. Your game should also override the scene’s didChangeSize(_:) method, which is called whenever the scene changes size. When this method is called, you should update the scene’s contents to match the new size.

download this page as .md

download this page as .pdf

back to SpriteKit documentation