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
Constraints in code make it very simple to layout our views, but it’s still often more cumbersome than Interface Builder because we have to write out a line of code for every single constraint. In this video, let's take a look at Auto Layout's visual format language and how we can write multiple constraints in one go.
-
0:00
[MUSIC]
-
0:04
Constraints in code especially when using layout anchors make it very simple to
-
0:08
layout our views.
-
0:10
But it's still often more cumbersome than interface builder because we have to write
-
0:14
out a line of code for every single constraint that we want to add.
-
0:19
And that can quickly add up, even for a very simple way out.
-
0:22
There's one final aspect of auto layout we need to explore
-
0:25
that solves this problem in an interesting way.
-
0:28
This final way we can build constraints is using text based visual strings.
-
0:34
This might sound weird, but Im sure you've seen art work like this at some point.
-
0:39
This is commonly known as ASCII art.
-
0:41
Because it's made from characters defined in the ASCII character standard.
-
0:45
This method of writing constraints is known as a visual format language.
-
0:49
And uses a text string to describe the layout.
-
0:52
Here we have another final empty project, so we can build another simple layout.
-
0:57
Actually, even though you're probably tired of the same example,
-
1:00
we're going to implement the orange, purple and blue view combo but
-
1:03
one last time, so that now we have three different approaches that you can compare.
-
1:08
So, like the last two times, let's go to the view controller, and
-
1:12
we'll start off with some boilerplate code.
-
1:16
So we need three views, let orangeView,
-
1:23
blueView, and a purpleView.
-
1:34
We also need to change their background colors, which we will do in viewDidLoad.
-
1:44
So we use the red, green, blue initializer on UIColor and for
-
1:49
red we're going to say 255/255.0 now this is for the orange view.
-
1:55
Green is 148.
-
1:57
Blue is zero and, that's just zero.
-
2:04
And the float value for alpha is 1.0 as always.
-
2:08
Next we have the purpleVew.
-
2:13
So for red this is 187.
-
2:16
For green, 44.
-
2:23
Blue is 162 and alpha is 1.
-
2:26
And then finally,
-
2:30
we have the blueView.
-
2:35
122 for red, 206 for green.
-
2:43
255 and one.
-
2:47
Okay, remember that whenever we add constraints in code we also need to set
-
2:51
translates auto resizing mask into constraints to false.
-
2:55
So we'll do all of that in the viewDidLoad.
-
2:56
So we'll say orangeView.translates equal false.
-
3:04
And then I'm just going to copy this and change the views.
-
3:10
So that's purple and blue.
-
3:13
And the last thing we need to do for
-
3:16
boilerplate at least, is to add these views as subviews.
-
3:19
So view.addSubview.
-
3:21
OrangeView.
-
3:23
purpleView and blue.
-
3:32
There we go.
-
3:33
Finally we can get started.
-
3:34
Now the first thing we're going to do, and to keep this simple we'll do everything
-
3:38
inside viewDidLoad, the first thing we're going to do is define a dictionary.
-
3:42
Now, this might seem a bit odd, but bear with me.
-
3:45
So we'll call this dictionary views.
-
3:48
And it's going to have a type of String to AnyObject.
-
3:54
The values in this dictionary are going to be the views
-
3:58
that we want to define our constraints on.
-
4:01
The first view we'll be adding constraints to is the orange view.
-
4:04
So the keys in this dictionary will describe the view that is being used
-
4:08
as the value.
-
4:09
So for the orangeView, we'll give a key of orangeView and
-
4:13
the value is the actual instance of UI view, the orangeView.
-
4:18
Now we can use this key orangeView as a string to refer to the actual view.
-
4:24
The benefit of using the visual format language is that we can create several
-
4:28
constraints in a single line of code by writing a sort of visual story.
-
4:33
To write a constraint using a format string we use a method on NS layout
-
4:37
constraint, Constraints with visual format that takes several different arguments.
-
4:42
So, we'll say NSLayoutConstraint.,
-
4:46
because this is a method on the class, constraints with visual format.
-
4:51
The first argument is a format strain.
-
4:53
And for now we're going to pass on an empty strain.
-
4:56
The next parameter, options, describes the attribute and
-
5:00
the direction of layout for all the objects in the visual format string.
-
5:05
We'll touch on this later.
-
5:06
And for now, we're going to put a set of empty brackets in here as an empty set.
-
5:11
The next parameter, named metrics,
-
5:14
takes another dictionary of constants that appears in the visual format string.
-
5:18
We're going to add these at the end so for now we'll just pass nil.
-
5:23
Finally we come to the views argument.
-
5:25
This is where we pass in our dictionary that we created.
-
5:28
So we'll say views.
-
5:31
This allows us to use the key in the format string.
-
5:35
That's this key, orangeView.
-
5:36
In this format string we're about to create to reference the orangeView.
-
5:41
The first constraint we're going to define is the with of the orangeView.
-
5:45
So let's go back to this first argument the format string.
-
5:49
Inside the string write exactly as I do.
-
5:52
So uppercase H colon open bracket then write orangeView,
-
5:59
then open parentheses, 200, and then close the parentheses and close the bracket.
-
6:06
And like that we've define a width constraint on the orangeView.
-
6:10
There's a lot of magic going on here.
-
6:12
So let's parse this string.
-
6:14
The first H and
-
6:16
a colon indicates that this format string specifies a horizontal constraint.
-
6:21
If this were a constraint in the vertical axis, we'd use an uppercase V over here.
-
6:27
If we omit both the H and the V and the colon, so if we did this,
-
6:32
Auto Layout would default to creating constraints in the horizontal direction.
-
6:36
Inside the format string,
-
6:37
after the orientation, we specify a view inside square brackets.
-
6:43
So here we're indicating that we have a constraint that involves the orangeView.
-
6:47
To give the orangeView a with constraint
-
6:51
all that's left to do now is to specify a value in parentheses.
-
6:55
So how does the system know that this is an actual with constraint?
-
6:59
That's because this value is provided in the horizontal axis.
-
7:02
This value inside the parentheses.
-
7:05
If this were a v, this value would be translated into a height constraint.
-
7:10
When we specify orangeView inside the format string, AutoLayout knows which view
-
7:14
we refer to because it uses this string as a key in the views dictionary.
-
7:19
And gets the actual view as a value.
-
7:22
That is the entire purpose of this views dictionary.
-
7:25
The first metric we're using here is the 200 value to specify a width constraint.
-
7:31
200 inside parentheses in a format string isn't a very meaningful description,
-
7:37
so let's improve on this.
-
7:38
To use descriptive names for
-
7:41
constants in the format string we use a metrics dictionary.
-
7:45
So like views this is a string to any object dictionary.
-
7:49
So we'll say string to any object.
-
7:54
And to start we'll pass in an empty dictionary.
-
7:57
Inside, we're going to specify a string that serves as a key for
-
8:01
the constant that we want to specify and a constant for the value.
-
8:06
So will say for example orangeViewWidth and
-
8:11
give it a value a constant value of 200.
-
8:15
Now we can pass in this dictionary as the argument to the metrics parameter.
-
8:22
And use this key instead of a magic number.
-
8:25
So will copy this and paste it here.
-
8:29
Before we move on to specifying more constraints,
-
8:31
we need to add these constraints to the actual view.
-
8:34
Because this method, constraintsWithVisualFormat,
-
8:37
allows us to specify many constraints in a single format string, the return value
-
8:43
on the method is an array of constraints, an array of NSLayoutConstraint.
-
8:48
So we can copy this, or we can pass this method
-
8:53
straight into views addConstraints method that takes an array of constraints.
-
8:57
So we'll view.addConstraints and then we'll pass this right in.
-
9:05
Okay so this was our first horizontal constraint on the orangeView.
-
9:09
There is one more to add in the horizontal direction and
-
9:12
that's the center X constraint on the orangeView.
-
9:15
But unfortunately centering views using the visual format
-
9:19
string method isn't exactly elegant.
-
9:21
So to get around this we're just going to use an anchor solution.
-
9:25
Which shows that we can mix and match different methods of adding constraints.
-
9:28
So right here, I'll say orangeView.centerXAnchor.constraintEqualT-
-
9:35
oAnchor.
-
9:36
And we're going to tie this to the view.centerXAnchor.
-
9:43
Okay, that was a fairly simple example, so let's add a few more.
You need to sign up for Treehouse in order to download course files.
Sign up