Bushers
Bushers

Reputation: 15

Setting background to cover whole screen when using aspect Fit in Swift

When using aspect fit in swift, the sides of the screen are removed. Is there a way I can cover this up by using a background colour to fill the whole screen or use a background image?

Here is the code for creating the scene as aspect fit in the GameViewController

if let view = self.view as! SKView? {

            if let scene = SKScene(fileNamed: "mainMenu") {


//                setting scene here to aspect fit

                scene.scaleMode = .aspectFit

//                 Present the scene
                view.presentScene(scene)
            }

            view.ignoresSiblingOrder = true

            view.showsFPS = true
            view.showsNodeCount = true
        }

scene with aspect fit and background set to clear

scene with aspect fill

scene with aspect fit and background set to green

Upvotes: 1

Views: 971

Answers (1)

user8350399
user8350399

Reputation:

Make sure you set the screen size for the scene.

let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
scene?.size = CGSize(width: screenWidth, height: screenHeight)

After, set scene.scaleMode = .aspectFill

If this doesn’t work, you can try and set the background color by doing this, but I’m unaware if this option will give you what you’re looking for as I didn’t test it.

scene?.backgroundColor = UIColor.white //color you want

Otherwise try setting scene?.size = image.size

Upvotes: 2

Related Questions