Let's get rid of storyboards in Xcode 12

Subscribe to my newsletter and never miss my upcoming articles

Hey there! I hope you are doing fine. If you are a storyboards fan, that's okay. I like storyboards, too. Storyboards simplify the process most of the time but sometimes you have to do things programmatically and when you do that it feels like you have the control, it just feels right.

You may ask: Why not SwiftUI, isn't it more elegant way of developing iOS apps? Yes, it is. I love SwiftUI but there is a lot of projects in the field right now that uses our good old friend UIKit and some of them don't even use Swift. Knowing how to do stuff in UIKit will still be a valuable skill in the near future.

In this article I want to start small and show you how to start a project without storyboards in Xcode 12. I'm stressing the version of it because it may change in the future or you might be we working on older versions of Xcode. For example if you are in a project that supports iOS 12 you can't even find a file called SceneDelegate because it came with iOS 13.

Let's fire up Xcode and create a sample project to work on. Name it whatever you want after the creation of project right click on Main.storyboard file on the left sidebar and select the delete option. Immediately you'll see a pop up shows up on your screen like below, choose the "Move to Trash" option.

Ekran Resmi 2021-01-22 15.12.58.png

After that go to the Info.plist file and with the help of CMD+F key combination, search for "Main". You'll see a row contains "Main" keyword. Click minus option and delete it.

Ekran Resmi 2021-01-22 15.19.17.png

After that click the title of the project on the left sidebar then under the General > Deployment Info you'll see an input box named "Main Interface" delete its value, too. This option tells the system where to look for default storyboard. Since we are not using one we can leave it empty.

Ekran Resmi 2021-01-22 15.23.12.png

We're close! Now go to SceneDelegate file and change the first method like below. As a root view controller, I'm using our default view controller that is created by Xcode after firing up our sample project.

func scene(_ scene: UIScene, 
willConnectTo session: UISceneSession, 
options connectionOptions: UIScene.ConnectionOptions) {
    guard let scene = (scene as? UIWindowScene) else { return }
    window = UIWindow(frame: scene.coordinateSpace.bounds)
    window?.windowScene = scene
    window?.rootViewController = ViewController()
    window?.makeKeyAndVisible()
}

Just to make sure, I'm going to change the background color of our default view controller to .systemGreen.

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .systemGreen
    }

}

Phew! That was quite a lot of steps. If you see the green background color when you run the project then the job is nicely done my friend. Now, slowly get away from your screen and just give a little break, rest your eyes for a bit. You deserve it! ✌️

Article cover photo by Abdelrahman Sobhy on Unsplash

No Comments Yet