set main window in swift

To set the main window in Swift, you need to do the following:

  1. Open the Main.storyboard file in your Xcode project.
  2. Select the view controller that you want to set as the main window.
  3. Go to the Attributes Inspector tab in the right panel.
  4. Check the box next to "Is Initial View Controller".
  5. Save your changes.

Alternatively, you can do this programmatically by setting the window property of your UIApplication object to a new window that contains your view controller:

main.swift
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        let viewController = MyViewController()
        window.rootViewController = viewController
        self.window = window
        window.makeKeyAndVisible()
    }
}
402 chars
10 lines

This creates a new window, sets its root view controller to your desired view controller, and then makes it visible.

gistlibby LogSnag