sprite-kit Create an SKScene that Fills the SKView


A simple use case it to create an SKScene that exactly fills the SKView. This avoids the need to consider scaling the view to fit or setting a camera to show a part of the scene.

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:

In Swift:

let sceneSize = CGSizeMake(skView.frame.width, skView.frame.height)
let scene = SKScene(size: sceneSize)


However if the SKView can change size (e.g. if the user rotates their device and this causes the view to be stretched because of its constraints) then the SKScene will no longer fit the SKView. You could manage this by resizing the SKScene each time the SKView changes size (e.g. in the didChangeSize method).