SpriteKit scene is sized/scaled incorrectly

Viewed 110

I'm trying to implement SpriteKit in my SwiftUI app, but the scene (a SwiftUI SpriteView) is sized and/or scaled incorrectly and I can't figure out how to solve the problem. The scene should be contained by the screen size, as you would expect.

My ContentView:

struct ContentView: View {
    var scene: SKScene {
        let scene = GameScene()
        let screenSize = UIScreen.main.bounds

        scene.size = CGSize(width: screenSize.width, height: screenSize.height)
        scene.scaleMode = .fill
        
        return scene
    }
    
    var body: some View {
        GeometryReader { geo in
            Group {
                SpriteView(scene: scene, transition: SKTransition.reveal(with: .down, duration: 1.0))
                    .ignoresSafeArea()
                    .frame(width: geo.size.width, height: geo.size.height)
            }
        }
    }
}

How can I make the scene properly fit the screen?

1 Answers

Try .aspectFill case instead of .fill:

scene.scaleMode = SKSceneScaleMode.aspectFill
Related