Personal website of developer Filip Stefansson / Archive

Fix SpriteKit scaleMode in landscape orientation

If you have this problem, you probably started your project from the SpriteKit template in XCode, yes?

If you look at the View Controller, in which you include the game scene, you’ll find this:

- (void)viewDidLoad
{
    \[super viewDidLoad\];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;
    
    // Create and configure the scene.
    SKScene * scene = \[MyScene sceneWithSize:skView.bounds.size\];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    
    // Present the scene.
    \[skView presentScene:scene\];
}

The simple solution is to just move all this code to the viewWillLayoutSubviews method. Like this:

- (void)viewWillLayoutSubviews
{
    \[super viewWillLayoutSubviews\];
	
	// Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;
    
    // Create and configure the scene.
    SKScene * scene = \[MyScene sceneWithSize:skView.bounds.size\];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    
    // Present the scene.
    \[skView presentScene:scene\];
}

Problem solved!

More articles →