How to set initial view controller for swiftUI

Viewed 4975

I normally use storyboard to set a view controller as initial view controller by clicking in the attribute inspector.

How can I set initial view controller in swift UI?

System info: Swift 5, Xcode 11.3.1.

2 Answers

In SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    let contentView = ContentView().environment(\.managedObjectContext, context)

    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: contentView)
        self.window = window
        window.makeKeyAndVisible()
    }
}

change the line let contentView = ContentView()... to YourInitialView()...

The result should look like this

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // change this line to your initial view controller class name
    let contentView = YourInitalView().environment(\.managedObjectContext, context)
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: contentView)
        self.window = window
        window.makeKeyAndVisible()
    }
}

You can not use UIViewController with SwiftUI, In SwiftUI Views are Struct.

Step to change Initial View in SwiftUI

  1. Open SceneDelegate (Not AppDelegate)
  2. Upate scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) func like below
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        let contentView = SignInInputView()

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }

where SignInInputView is Stuct like below

struct SignInInputView: View {
    @State private var userNameString: String = ""
    @State private var passwordString: String = ""
    var body: some View {

        HStack {
            Spacer(minLength: 20)
            VStack(alignment: .leading, spacing: 20) {
                TextField("Enter UserName/Password", text: $userNameString, onEditingChanged: { (value) in
                    print(value)
                }, onCommit: {
                    print(self.userNameString)
                }).frame(height: 60)
                    .padding([.leading, .trailing], 20)
                    .background(Color.red)

                TextField("Enter Password", text: $passwordString, onEditingChanged: { (value) in
                    print(value)
                }, onCommit: {
                    print(self.passwordString)
                }).frame(height: 60)
                    .padding([.leading, .trailing], 20)
                    .background(Color.green)
            }
            Spacer(minLength: 20)
        }
    }
}
Related