Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Adding User Interface Elements
5:57 with Nicole HinckleyNew concepts in programming can be difficult, but in some cases they're as easy as a drag and drop! In this video we will learn how to add some UI Elements onto our scene. We will also learn how to use the Attribute Inspector to customize our elements to match our design.
-
0:00
Let's reference back to our design and see what we need to do.
-
0:03
We have three main UI elements to create.
-
0:06
The Did you know text up top.
-
0:09
Our fun fact text in the middle.
-
0:11
And the button down at the bottom that,
-
0:13
when we tap it, will show another fun fact.
-
0:16
Let's start with the fun fact text in the middle.
-
0:19
If you click on this icon up top that looks like a square inside of a circle,
-
0:23
that will open up what's called the object library.
-
0:27
If we scroll down, we can see some of Apple's stock UI elements that might
-
0:31
look familiar if you've used iOS devices before.
-
0:35
Like a slider, a switch, an activity indicator, and so on.
-
0:41
The object library comes packed with a lot of UI elements that we can use right out
-
0:45
of the box.
-
0:46
What we're looking for is a label to display text.
-
0:49
We could scroll through everything in the list, but
-
0:52
there are quite a few objects here.
-
0:54
So instead, we can just search for the name.
-
0:57
So in this case, we want a label.
-
1:00
Now, all we're gonna do is drag and drop it into our view.
-
1:03
So let's click, and drop.
-
1:06
Perfect, easy, right?
-
1:08
We can see that our label is selected, so now if we look over in our attributes
-
1:12
inspector, we can see some attributes that are specific to a label.
-
1:17
We wanna change what text is shown by the label.
-
1:20
So if we come here where it says Label, underneath where it says Text here,
-
1:25
we can type in a random fact as some dummy text for now.
-
1:30
Let's hit Enter.
-
1:32
And you'll see your label automatically updates in the interface builder.
-
1:36
Now, let's make sure our label is centered in the middle of our U,
-
1:39
just like in our design.
-
1:41
If we click and
-
1:42
drag our label near the middle of the screen, we'll see some lines appear.
-
1:47
These lines indicate that our label is being centered vertically and
-
1:51
horizontally.
-
1:52
So once we have both lines showing up, we know our label's in the right spot.
-
1:57
Next up, let's get our button onto the screen.
-
1:59
We need to grab one from our Object Library again.
-
2:02
This time, let's do it a little differently.
-
2:04
Xcode has a lot of really useful shortcuts that can help improve our workflow.
-
2:09
To open up the Object Library with a keyboard shortcut,
-
2:12
use the combination of Cmd+Shift+L.
-
2:16
Nice.
-
2:17
Let's search for our button, and drag and drop it underneath our label.
-
2:21
So button.
-
2:23
And just like before, let's drag and drop.
-
2:27
On our design, we see that this button needs to say show another random fact.
-
2:32
With our button selected here, let's check out the Attributes Inspector again.
-
2:36
If you're ever wondering what kind of attributes you can change on a UI element,
-
2:41
the Attributes Inspector is a great place to check.
-
2:44
It looks like buttons can have things like titles, text colors, and images.
-
2:49
I wanna change the title of the button, which is currently set to Button, and
-
2:53
change it to Show Another Fun Fact.
-
2:58
And hit Enter.
-
2:59
Finally, we need another label for our Did you know text above our fun fact label.
-
3:04
How about this?
-
3:06
Go ahead and try to do this on your own.
-
3:08
Post the video, and once you've tried to do it yourself, un-pause and
-
3:12
we'll continue along together again.
-
3:14
Remember, practice makes perfect.
-
3:16
So give it a go.
-
3:19
All right, so again,
-
3:21
I'll pull up the Object Library using the Cmd+Shift+L shortcut.
-
3:26
Search for a label, and drag and drop it above our fun fact text.
-
3:32
Over in the Attributes Inspector,
-
3:35
with the label selected, let's change this text to Did you know.
-
3:40
Hopefully, you were able to do it.
-
3:41
If not, don't worry, this will all become second nature eventually.
-
3:46
Finally, let's move our label into the center.
-
3:50
There we go.
-
3:52
And I wanna center the alignment of the text.
-
3:55
So in the Attributes Inspector, if we look right here, we can see Alignment.
-
3:59
And let's hit this button here to center our alignment.
-
4:03
We've done some really great work here.
-
4:06
I wanna see how our app is looking on the simulator.
-
4:08
But now that we've got some UI elements on the screen and not just a background
-
4:12
color, we really need to make sure that the simulator that we've selected in our
-
4:16
tool bar is the same size as the screen we've been viewing the Interface Builder.
-
4:22
Let me show you why.
-
4:23
If we look at this button down at the bottom,
-
4:25
we can see that it says here that we're currently viewing as an iPhone 8.
-
4:29
The iPhone 8 is a 4.7 inch screen size,
-
4:32
which means that we've been designing specifically for a 4.7 inch screen.
-
4:38
If we select another device by clicking here, and
-
4:41
selecting the iPhone SE, which is much smaller,
-
4:47
the screen size shrinks down and our UI is all out of place now.
-
4:51
Selecting an iPad really shows this off.
-
4:55
Let's scroll out a little bit here by hitting this minus button.
-
4:59
And you can see that our UI elements are nowhere near the center.
-
5:03
This is because we didn't give any guidelines for what our UI element should
-
5:07
do to adapt when it's being displayed on different phone sizes.
-
5:11
Luckily for us, there are some really awesome ways to make sure that our UI
-
5:15
works on all different screen sizes.
-
5:17
But for now, for simplicity's sake, let's put our preview back to the size that we
-
5:22
designed our UI on, which in my case was the 4.7 inch iPhone 8.
-
5:27
Up top, let's make sure that our simulator is a 4.7 inch model.
-
5:32
So I've got the iPhone 8 selected here, and let's run the app.
-
5:40
And there it is.
-
5:41
We've had our two text labels and our button down here at the bottom.
-
5:45
You can even tap on this button, and
-
5:47
it will give you a nice little animation showing it's being interacted with.
-
5:50
Our button doesn't do anything useful when we tap it yet.
-
5:54
So up next, we'll be hooking up our UI to some code.
You need to sign up for Treehouse in order to download course files.
Sign up