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.swift
The file is responsible for the startup and termination of the App, and is responsible for the connectionSceneDelegate
Handover.SceneDelegate.swift
Files are responsible for managing the application’s life cycle.
Keep SceneDelegate
AppDelegate
Through theapplication(_:configurationForConnecting:options)
Returns aUISceneConfiguration
The instance- Upon completion of the startup, control is transferred to
SceneDelegate
, it’sscene(_:willConnectTo:options:)
It’s going to be called, setwindow
The 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
-
Delete SceneDelegate. Swift
-
Delete the following information in info.plist
-
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
- delete
AppDelegate.swift
The 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