Xcode 11 & iOS13, using UIKIT can’t change background colour of UIViewController

and commented the code present in xxx scene delegate to not have the UIKit part

You mustn’t do that. It is your code that needs to go in the right place. If you make a new project in Xcode 11, this code does nothing:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window = UIWindow()
    window?.makeKeyAndVisible()
    let controller = MainVC()
    window?.rootViewController = controller
    return true
}

The code runs, but the window property is not your app’s window, so what you’re doing is pointless. The window now belongs to the scene delegate. That is where you need to create the window and set its root view controller.

func scene(_ scene: UIScene, 
    willConnectTo session: UISceneSession, 
    options connectionOptions: UIScene.ConnectionOptions) {
        if let windowScene = scene as? UIWindowScene {
            self.window = UIWindow(windowScene: windowScene) 
            let vc = MainVC()                                  
            self.window!.rootViewController = vc             
            self.window!.makeKeyAndVisible()                 
        }
}

Leave a Comment