Gerard G
Gerard G

Reputation: 3463

Background is not filling the whole view SpriteKit

For some reason my code will not fill the whole SKScene. Here is the code that I am using on Xcode 12 Beta 5.

GameScene.swift

class GameScene: SKScene {
 
    override func didMove(to view: SKView) {
        let background = SKSpriteNode(imageNamed: "space")
        background.zPosition = 0
        background.anchorPoint = CGPoint(x: 0.5, y: 0.5) // default
        background.position = CGPoint(x: frame.midX, y: frame.midY)
        print("frame.size  \(frame.size)")
        print("self.size  \(self.size)")
        print("view  \(view.frame.size)")
        background.size = CGSize(width: self.size.width, height: self.size.height)
                self.addChild(background)
    }

}

GameViewController.swift

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        

        if let scene = GKScene(fileNamed: "GameScene") {
            
            if let sceneNode = scene.rootNode as! GameScene? {


                // Present the scene
                if let view = self.view as! SKView? {
                    sceneNode.size = view.bounds.size
                    sceneNode.anchorPoint = CGPoint.zero
                    sceneNode.scaleMode = .aspectFit
                    
                    print("view.bounds.size  \(view.bounds.size)")
                    view.presentScene(sceneNode)
                    
                    view.ignoresSiblingOrder = true
                    
                    view.showsFPS = true
                    view.showsNodeCount = true
                }
            }
        }
    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .allButUpsideDown
        } else {
            return .all
        }
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }
}

Also for some reason, my view size is reporting ->

frame.size (320.0, 480.0) self.size (320.0, 480.0) view (320.0, 480.0)

But my GameScene.sks is set to -> w750, h1336

Why is my code cutting off the tops and the bottoms of the backsimulator screen shotground?

Upvotes: 3

Views: 1108

Answers (2)

Ilari Niitamo
Ilari Niitamo

Reputation: 171

This is definitely caused by not having a launch screen assigned. It's very odd that Xcode 12 has this behavior for SpriteKit by default. I'm sure many many people will be stumped and confused. I noticed that the main visible difference to earlier versions was the lack of a launch screen.

You can either create a launch screen and assign it or define Main.storyboard as the launch screen, as proposed earlier. Probably the easiest thing to just get it working is to go to the project target, then General and choose "Main" where it says "Launch Screen File". This will make it work as expected.

Update: This is still happening in Xcode 12.4 (12D4e). Surprisingly, the launch screen will be missing in a brand-new iOS-only project, whereas it's there in a multi-platform project. It seems like this is an oversight on Apple's part.

How to select Main as your project's Launch Screen File:

How to select Main as your project's Launch Screen File

Upvotes: 4

Lucas Dahl
Lucas Dahl

Reputation: 742

This is going to sound dumb, but do you have a launch Screen? I was having the same problem which would only happen in Xcode 12 and not 11, but the main difference I found was Xcode 11 has a launch screen. Once I added the launch screen storyboard and added it to my plist my SCNView would fill the screen. I am not sure why this would cause the view not to follow the constraints, but after adding it to my other projects it fixes the issue.

Edit: You do not need to have a launch screen storyboard, you can just add your main storyboard that displays you scene in the plist under "Launch screen interface file base name".

Upvotes: 12

Related Questions