How to Work with Programmatic View Controllers in Xcode 12

After Xcode 12’s release, I had a mini heart attack when I tried to create a new project. This is what I am talking about:

New project, okay so far so good
What’s this?! Where’s my UIKit?!!

It has always been known that SwiftUI is intended to be the replacement for UIKit as iOS’s goto UI framework, but the change was meant to be gradual. While intuitive and easy to use, Swift UI in its current form is missing plenty of features that UIKit has to offer.

I thought I would have way more time before the transition. Needless to say, Xcode 12’s change to offer SwiftUI by default really caught me off guard! I was also one of those people who did everything UI related programmatically, so having storyboard there as an option did not help.

If you’re like me, and would like to stick to UIKit just a little bit longer, maybe because you have an established code base with UIKit, or maybe you’re not yet comfortable with SwiftUI and find UIKit much more suitable to your needs, then read on. I will be showing you two different methods to initiate a new Xcode project, and start working with UIKit programmatically in Xcode 12.

Method 1: Initiate Interface with SwiftUI

Step 1: Select SwiftUI as the option for your Interface in the new project. Also make sure you choose UIKit App Delegate for the Life Cycle option.

Step 2: Delete ContentView.swift, and create your own view controller file. Name your view controller however you like.

Delete this
Create a file with this

Step 3: Navigate to SceneDelegate.swift, and change its contents to use your new view controller as the starting point instead.

Your app’s starting point
Change this function
To this

And you’re done! Run your build on a device or a simulator and it should work.

Method 2: Initiate Interface with Storyboard

Step 1: Select Storyboard as the option for your Interface. Also make sure your Life Cycle is selected as UIKit App Delegate.

Step 2: Delete the Main.storyboard file, and remove the reference to it from your project setting.

Delete Main.storyboard
Delete the Main from Main Interface and leave it empty

Also, navigate to Info.plist, and delete the reference to Main.storyboard there.

Delete this line

Step 3: Similar to before, navigate to SceneDelegate.swift, and change its contents to use a view controller as the starting point. You should also already have an UIViewController class created for you. Feel free to change the name of this class.

Change this function
To this

Run your build and test your programmatic view controller.

And that’s it! If there’s any error, make sure you’ve followed through with all of the steps above for either methods. Good luck!

Software Engineer, I also play board games a lot :D