swift - Change the default black background color


Keywords:swift 


Question: 

I'm using .aspectFit in an app I'm creating, and as a result of using .aspectFit the entire scene doesn't fill up the screen (which is what I want). However, I would like to change the black back color to red if possible. Currently my scene background color is green and the back color is black (which must be the default).

I've done a little reading on the internet and it has been suggested that the black back color can be changed in the AppDelegate, however nothing seems to happen when I use the below code (the back color is still black).

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    self.window?.backgroundColor? = UIColor.red

    return true
}

enter image description here

Am I changing the wrong background color, or am I missing something? Any help would be appreciated.


1 Answer: 

The scale mode aspectFit scales the contents equally in both dimensions and ensures that all of the scene’s contents are visible.

The scaling factor of each dimension is calculated and the smaller of the two is chosen. Each axis of the scene is scaled by the same scaling factor. This guarantees that the entire scene is visible but may require letterboxing in the view.

To change the background color of your scene you don't need to change the window color, you simply can do:

scene.backgroundColor = .black

If you want to chage the window background color you can do it also in your scene with:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.backgroundColor = .red