First iOS App with Programmatic UI — 2023 Edition
In this tutorial I will show you how simple it actually is to abandon storyboard and create your UI completely in code using UIKit 🔥
Watch me stream iOS Development on Twitch here!
Index
1. Create a new project
2. Delete all unnecessary files and references
3. Add root ViewController
4. See your first UI change using code
1. Create a new project
We’ll start by creating a new iOS project — there’s no better feeling than creating a fresh project 😍
That’s it for the first chapter — on to the next one!
2. Delete all unnecessary files and references
This is a step I love to do: deleting the Main.storyboard 🙌🏻✨
Make sure to “move to trash” when deleting the file.
If you’re trying to run the app now it will build successfully but crash and throw an error because we still have a reference to the now deleted Main storyboard in our project settings.
Let’s fix this crash by removing the last two references to the Main storyboard. First click on the info.plist and select the row that is called “Storyboard Name” and hit the minus.
Next click on the blue project file and go to the info tab. Here tap on the row that is called “Main storyboard file base name” and hit the minus.
And that’s it! If you run your project now it won’t crash. Perfect time to move to the next chapter!
In case your App still crashes try to hit cmd + shift + k to clean the build folder.
3. Add root ViewController
In this chapter we are de-mystifying what the storyboard actually does for us by doing it ourselves — and it’s surprisingly simple 💪🏻
The entry point of our App is the file SceneDelegate.swift. Let’s go there and reduce the noise by deleting all functions except the one you see down below:
We will have to do 3 things. For One we have to instantiate a new UIWindow and assign it to the local “window” property of the SceneDelegate class. Two we instantiate the first view controller that we want to display when the app launches. This is the view controller we are going to assign as the rootViewController property of “window”. Finally Three we will make the window “key” and “visible”. Basically telling our app that this is our active window.
import UIKitclass SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow? func scene(
_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions
) {
guard
let windowScene = (scene as? UIWindowScene)
else {
return
} // One
self.window = UIWindow(windowScene: windowScene) // Two
self.window?.rootViewController = ViewController() // Three
self.window?.makeKeyAndVisible()
}
}
4. See your first UI change using code
In our ViewController.swift file we will change the background color of our view to really see that we can make changes to the UI! For that add the following line:
import UIKitclass ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad() view.backgroundColor = .red
}
}
Now we are able to run the app and see a beautiful view with red vibrant background 😊!
You successfully implemented your first view programmatically 🎉!
Watch me stream iOS Development on Twitch here!