Programmatic constraints is a way of laying out and constraining views as opposed to using Storyboards. While more intimidating at first, programmatic constraints can be a faster way to create user interfaces. It just takes some practice.
When creating a new Xcode project, there isn’t a nice preset dropdown option that will configure a boiler plate programmatic project for us.
Here are 5 steps to get your programmatic project set up and leave storyboards behind like a bad ex.
1. TRASH THE MAIN.STORYBOARD
Delete and forget it. You deserve better and want some consistent contraints in your life.
(Swift UI takeover is coming…)
Remember Main.Storyboard? Me neither. Delete it’s baggage:
- Storyboard Name
- Main storyboard file base name
3. Main Interface: LaunchScreen.storyboard
In your project’s target file set Main Interface to: LaunchScreen.storyboard
While you moved on from your main.storyboard, you’re still cool with their brother LaunchScreen so you keep in touch.
In SceneDelegate.swift, under the func scene(willConnectTo:) configure the root view controller to be your CustomViewController.
Here are a couple examples of different controllers you would commonly use:
To test if your rootViewController is linked up correctly, go to your ViewController file and change the background color of your view.
We have re-found ourself and can now make changes as we please without IBOutlet’s permission.
- Delete Main.storyboard.
- Remove Storyboard Name and Main storyboard file base name from Info.plist.
- Set Main Interface to LaunchScreen.storyboard in the target of your project file.
- Set the windowScene rootViewController to your CustomViewController in the SceneDelegate.Swift.
- Change the background color on your CustomViewController.