How to programatically set a view controller as root view controller in Xcode using Swift?

Let us say that our view controller is called as: mainViewController.

Screen Shot 2018-08-10 at 5.53.34 PM

In order to make the mainViewController as our root view controller follow the steps below:

Step 01 : we have to include the following code inside the application (didFinishLaunchingWithOptions) function in our AppDelegate.swift file .

let mainVC = mainViewController()

window = UIWindow()

 window?.rootViewController = mainVC

 window?.makeKeyAndVisible()

Screen Shot 2018-08-10 at 6.04.18 PM

Step 02: Delete the Main.storyboard file

step 03: Click your project. Go to Targets > General> Main interface. Delete the word ‘Main’.

Now run the application.Your custom view controller will be set as the root view controller.

Advertisements

Leave a Reply