Search code examples
swiftuikit

Black when screen when settings initial controller programmatically


Could you please look at repo https://github.com/Rukomoynikov/InitialViewControllerProgrammatically and help me. Why I got an black screen when trying instantiateViewController.

This is my AppDelegate:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow.init(frame: UIScreen.main.bounds)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
        guard window != nil else { return true }
        self.window!.backgroundColor = .darkGray
        self.window!.rootViewController = viewController
        self.window!.makeKeyAndVisible()

        return true
    }
}

Couple details.

  1. App created in last Xcode version.
  2. iOs deployment target changed from 13 to 12.
  3. SceneDelegate removed.
  4. In target settings option Main Interface cleared.
  5. In info.plist StoryBoardName and DelegateClassName also removed.

Solution

  • The problem is that, in an attempt to make the project stop using the scene delegate and use the app delegate instead, you mangled the UIApplicationSceneManifest entry in the Info.plist. Instead, you would need to delete that entry entirely. Its mere presence is what causes the scene delegate architecture to be used.

    It would be better, however, to make this work for both iOS 12 using an app delegate and iOS 13 using a scene delegate (as I have described at https://stackoverflow.com/a/58405507/341994).