Max B
Max B

Reputation: 375

Scene with SpriteView and SwiftUI does not fill whole screen

I am trying to work on Project 1 and use SpriteKit and SwiftUI but for some reason the scene never fill the whole screen on my iPad.

import SwiftUI
import SpriteKit

struct ContentView: View {
    var scene: SKScene {
        let screenWidth  = UIScreen.main.bounds.width
        let screenHeight = UIScreen.main.bounds.height
        
        let scene = GameScene()
        scene.size = CGSize(width: screenHeight, height: screenWidth)
        scene.scaleMode = .fill
        scene.backgroundColor = .green
        return scene
    }
    
    var body: some View {
        let screenWidth  = UIScreen.main.bounds.width
        let screenHeight = UIScreen.main.bounds.height
        
        print(screenWidth)
        
        return SpriteView(scene: scene)
            .frame(width: screenWidth, height: screenHeight)
            .ignoresSafeArea()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Right now it looks like this:

Any ideas what I am missing here?

Max

Upvotes: 2

Views: 1091

Answers (2)

Max B
Max B

Reputation: 375

Fixed it in the end by putting the background in the ZStack() of the ContentView

var body: some View {
        let screenWidth  = UIScreen.main.bounds.width
        let screenHeight = UIScreen.main.bounds.height
        
        ZStack{
            EmptyView()
            Image("road")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .ignoresSafeArea()
            
            SpriteView(scene: scene, options: [.allowsTransparency])
                .frame(width: screenWidth, height: screenHeight)
                .ignoresSafeArea()
        }
    }
}

For some reason it won't blow up the texture to full screen size when I add it as part of the GameScene class, no matter what scale I try:

let background = SKSpriteNode(imageNamed: "road")
        
        background.zPosition = -1
//      background.scale(to: CGSize(width: 1180, height: 700))
//      background.scale(to: CGSize(width: CGFloat(1180), height: CGFloat(820)))
        addChild(background)

Upvotes: 3

sheldor
sheldor

Reputation: 159

You have height at width and width at height parameter at scene property. (How @aheze mentioned in the comment)

Upvotes: 0

Related Questions