sprite-kit Create an SKScene that Scales to fit the SKView


Example

An SKScene has a scaleMode parameter that defines how it will change its size to fit within the SKView it is presented into the SKView if it is not the same size and/or shape.

There are four options for scaleMode:

  • AspectFit: the scene is scaled (but not stretched) until it fits within the view. This ensures that the scene is not distorted but there may be some areas of the view that are not covered by the scene if the scene is not the same shape as the view.
  • AspectFill: the scene is scaled (but not stretched) to fill the view completely. This ensures that the scene is not distorted and that the view is completely filled but some parts of the scene may be cropped if the scene is not the same shape as the view.
  • Fill: the scene is scaled (and if necessary stretched) to fill the view completely. This ensure that the view is completely filled and that none of your scene is cropped but the scene will be distorted if the scene is not the same shape as the view.
  • ResizeFill: the scene is not scaled at all but rather its size is changed to fit the size of the view.

The following code assumes an SKView called skView already exists (e.g. as defined in Create a Full Screen SKView using Interface Builder) and a subclass of SKScene called GameView has been defined and then uses the AspectFill scaleMode:

In Swift 3:

    let sceneSize = CGSize(width:1000, height:1000)
    let scene = GameScene(size: sceneSize)
    scene.scaleMode = .aspectFill

    skView.presentScene(scene)