2.1. Converting the storyboard to code
So, let's convert the Storyboard to Swift code!
The following image is how the grid on the device screen is defined.


In the above image, we can see how the UI elements are placed on the device's screen in our App1.
We will add the code directly to ViewController.
Understanding the attributes
Label attributes
If you remember, In App1, we dragged and dropped a Label from the Objects Library and changed a few things. Let's go back to the Storyboard and click on the "Hello World!" label.

We changed the text to "Hello World!"
The color of the text is Blue (accent color).
The font is the system's default font, and the size is 24.
There are many other attributes we have in a Label that we can edit.
Okay, let's programmatically create a Label. And then set the Attributes. For this purpose, we will create a new project named "App_NoStory." We will not touch the Main storyboard here.
Open the ViewController code directly and add a variable labelHello of the UILabel.
Then inside viewDidLoad() we will have to define the attributes for labelHello.
In the above code, we defined the attributes for the label:
labelHello.textis the text inside the Label.labelHello.fontis the font of the Label. We created a UIFont object with the system's default font of size24.0. Then set the Label's font to that object.labelHello.textColoris the color of the text of the Label. We set the color to system's default Blue color. You can put a.and choose other options.labelHello.textAlignmentis the alignment of the text. We set the alignment to center. You can put a.and choose other options.labelHello.translatesAutoresizingMaskIntoConstraintsis set tofalsewhich is a default and fail-safe attribute for all the UI elements. The storyboard sets it automatically. If you write codes to edit the attributes, you have to set it tofalsemanually for every UI element. This attribute says to the iOS system that you are dynamically using your own constraints to display the contents on the screen. Otherwise, the system will try to pack all the UI elements together to display them on the screen without your choices.view.addSubview(labelHello)is where you add the logical view oflabelHelloyou created as a subview of the screen. Hereviewis the logical view of the screen. SincelabelHellois a child view ofview, we write this line of code.
Setting the constraints
Now that we have created a logical view of labelHello, we have to define the constraints. We can use NSLayoutConstraint system class to set the constraints. NSLayoutConstraint.activate() takes in an array of constraints and activate them on the current view. We know that labelHello has two constraints:
Centered to the x-axis (horizontal axis).
There is a gap of 32 points between the screen's top edge and the Label's top edge.
So, we can create the array of constraints with the above constraints and activate them by writing:
labelHello.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 32) means:
Set the
labelHello's top anchor to the top anchor of the safe area. (Remember, 'safe area'?) We wanted to have a 32 points gap,constantis the gap here.Set the
centerXAnchor(horizontal center point) oflabelHelloto the horizontal center point of the safe area.
Now let's see the entire code:
So we write a separate method initConstraints() to activate the constraints and call it from viewDidLoad(). Now let's run the app:

So, our program rendered a "Hello World!" label on screen!
Now, let's add all the constraints for the other two UI elements.
Let's see the code after we add all the constraints:
So, we call the methods to define the attributes of UI elements first, then activate the constraints.
Hint: You can still use the Storyboard to check what attributes to set up. When you are designing by coding for the first time, you can go to the Storyboard, place a UI element from the Objects Library on the Storyboard, and design it as you like. Then, use the attributes you edited to setup the design programmatically in ViewController. Then, delete the object from the storyboard. The Storyboard potentially becomes your draft design pad.
Adding button action
Remember, we added an event listener for the button tap using buttonClickMe.addtarget()? Where do you think we should add that target?
We can do that in the viewDidLoad() method since that is the logical Controller of the app:
And then, we use the same methods we wrote in 'App1'.
So the full code is:
If you run the app, you can see that we replicated the whole App1 by just writing codes. You should start practicing this early since almost everyone in the industry got rid of Storyboards.
Last updated
Was this helpful?