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:

Image for post
Image for post
New project, okay so far so good
Image for post
Image for post
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.

Image for post
Image for post

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

Image for post
Image for post
Delete this
Image for post
Image for post
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.

Image for post
Image for post
Your app’s starting point
Image for post
Image for post
Change this function
Image for post
Image for post
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.

Image for post
Image for post

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

Image for post
Image for post
Delete Main.storyboard
Image for post
Image for post
Delete the Main from Main Interface and leave it empty

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

Image for post
Image for post
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.

Image for post
Image for post
Change this function
Image for post
Image for post
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!

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store