After the release of Xcode 11, there will be many changes when new iOS projects are created. The biggest change is the addition of the file SceneDelegate. At this time, if you want to set up the interface through pure code, the process will be different from the past.

Pure code conditions

To delete Main in Main Interface, you need to delete the following code in info.plist

<key>UISceneStoryboardFile</key>
<string>Main</string>
Copy the code

Project file changes

  • AppDelegate.swiftThe file is responsible for the startup and termination of the App, and is responsible for the connectionSceneDelegateHandover.
  • SceneDelegate.swiftFiles are responsible for managing the application’s life cycle.

Keep SceneDelegate

  1. AppDelegateThrough theapplication(_:configurationForConnecting:options)Returns aUISceneConfigurationThe instance
  2. Upon completion of the startup, control is transferred toSceneDelegate, it’sscene(_:willConnectTo:options:)It’s going to be called, setwindowThe root view controller of
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    / / create the window
    self.window = UIWindow(windowScene: windowScene)
    // Set the Window rootViewController
    self.window? .rootViewController =ViewController(a)self.window? .makeKeyAndVisible() }Copy the code

Don’t keep SceneDelegate

  1. Delete SceneDelegate. Swift

  2. Delete the following information in info.plist

  3. The code in appdelegate. swift is written as it was before Xcode11

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    / / create the window
    self.window = UIWindow(frame: UIScreen.main.bounds)
    // Set the Window rootViewController
    self.window? .rootViewController =ViewController(a)self.window? .makeKeyAndVisible()return true
}
Copy the code
  1. deleteAppDelegate.swiftThe following code in
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {}
Copy the code