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
In Interface Builder layout guides are no more than predefined objects we interact with to set up our views but in code we can do much more. In this video let's explore what layout guides have to offer.
Documentation
-
0:00
When we learned about the basics of auto layout we covered some aspects of
-
0:04
layout guide.
-
0:05
If you're unfamiliar with that content follow the link list below.
-
0:09
A layout guide, as we previously discussed,
-
0:12
is an empty rectangle that we can use to help build layouts.
-
0:16
In Interface Builder we're restricted
-
0:19
to only being able to use the predefined layout guides, like the top and
-
0:24
bottom layout guides available on a view controller, but only prior to iOS 11.
-
0:29
In code, however, we can do a bit more with layout guides.
-
0:34
So, let's attempt to build a layout here with three views.
-
0:38
The goal is to space the three views out vertically
-
0:41
with equal spacing between the views, like so.
-
0:45
This sort of layout is achievable using stack views, but
-
0:48
stack views are a fairly new thing, and
-
0:50
if you're targeting older versions of iOS, you won't be able to use them.
-
0:54
Plus, it's always good to know all the tools you have on hand.
-
0:58
So in my project I've added a new swift file named LayoutGuides, and
-
1:03
in here we have a view controller named LayoutGuidesController.
-
1:08
Inside the class I have defined three views,
-
1:11
added it to the view hierarchy, and given it some constraints.
-
1:16
You can find this set up code in a link in the teacher's notes to this video.
-
1:21
Now to create the layout we want, the first thing we need is layout guides.
-
1:26
So I'm going to add a comment here.
-
1:28
// Layout Guides, and we'll define four stored properties and
-
1:33
assign a layout guide to each of them.
-
1:35
So, let firstLayoutGuide = UILayoutGuide,
-
1:43
and then I'm just going to copy this three times, paste it here.
-
1:48
I'll say, second.
-
1:51
Third and four.
-
1:58
Using layout guides is pretty straightforward.
-
1:59
You instantiate a new layout guide as we just did, and
-
2:03
add the layout guide to the view.
-
2:05
Let's do that now.
-
2:07
Layout guides function sort of as dummy views, but they aren't views, so
-
2:11
we don't add them as sub views.
-
2:12
We don't want them in our view hierarchy.
-
2:15
Instead, we have a method on UI view named
-
2:18
view.addLayoutGuide that allows us to add a layout guide, but
-
2:22
not involve them in the view hierarchy so that we don't have to draw a view.
-
2:27
So here we'll say firstLayoutGuide and we'll do this for all of them.
-
2:31
AddLayoutGuide(secondLayoutGuide), view.addLayoutGuide(thirdLayoutGuide),
-
2:37
and the last one.
-
2:40
Next we need to define the position and
-
2:43
size of each layout guide using auto layout.
-
2:46
If you, again, think of a layout guide as an invisible rectangle,
-
2:50
or a dummy view, much like regular views, they need to have a position and size.
-
2:57
We'll add these constraints in the activate method on NSLayoutConstraint.
-
3:02
So, right at the bottom, after all these, so these constraints here,
-
3:06
they simply give each of our views a height and a width, and
-
3:09
then position them or align them to the center x-axis.
-
3:13
So, we can add the rest of our constraints here in a new activate method,
-
3:18
NSLayoutConstraint.activate, and we'll start by placing
-
3:23
the first layout guide at the very top of our column of views.
-
3:27
So firstLayoutGuide.topAnchor.constraint(equ-
-
3:31
alTo: -- view.safeAreaLayoutGuide,
-
3:35
and obviously now you know that the safeAreaLayoutGuide is also
-
3:40
a layout guide, which is an empty rectangle, topAnchor.
-
3:44
It's just a pre-defined one.
-
3:47
Next, we'll pin the bottom of this layout guide to the top of the first
-
3:52
view we want to place, so view A.
-
3:54
So firstLayoutGuide.bottomAnchor.constraint(-
-
4:00
equalTo: viewA.topAnchor).
-
4:04
Each of these layout guides is going to go in between a view, so above and below it.
-
4:10
So now step two is to place the seond layout guide in between view A and
-
4:14
view B, and to do that, we'll take the topAnchor, of the secondLayoutGuide,
-
4:19
and pin it to viewA.bottomAnchor.
-
4:24
Then, next we'll do secondLayoutGuide.bottomAnchor and
-
4:28
this one we want to pin to viewB.topAnchor.
-
4:33
If this is confusing, you can put comments in between these things to say
-
4:38
which layout guide or which view is going in between what.
-
4:42
In between view B and view C we'll place the third layout guide.
-
4:45
So, thirdLayoutGuide.topAnchor.constraint(equ-
-
4:49
alTo: viewB.bottomAnchor), and then the layout guides,
-
4:54
so we're still on the third one,
-
4:57
it's bottom anchor is going to be pinned to viewC.topAnchor.
-
5:02
So just basic pattern here, and
-
5:04
finally we'll place the fourth layout guide after viewC.
-
5:09
So fourthLayoutGuide.topAnchor.constraint(eq-
-
5:14
ualTo: viewC.bottomAnchor), and finally the last anchor here, so
-
5:20
the fourth layout guide's bottom anchor is going to be pinned
-
5:25
back to the view.safeAreaLayoutGuide, and this time we want the bottomAnchor.
-
5:32
The trick to get this to work out is to set all the layout guides to have equal
-
5:36
heights.
-
5:37
This way after deducting all the heights of the views that we care about,
-
5:41
auto layout can take the remaining space and evenly divide it between the four
-
5:46
layout guides resulting in equal spacing above and below the views.
-
5:51
So a few more constraints here.
-
5:52
We'll say fourthLayoutGuide.heightAnchor.constraint-
-
5:58
(equalTo: thirdLayoutGuide.heightanchor).
-
6:02
Then we'll say thirdLayoutGuide.heightAnchor(equalTo:
-
6:06
secondLayoutGuide.heightAnchor, and we'll just walk up the chain this way.
-
6:12
SecondLayoutGuide.heightAnchor.constraint- (equal to:
-
6:17
firstLayoutGuide.heightAnchor), and then for the last one we'll
-
6:22
say firstLayoutGuide.heightAnchor(equalTo: fourthLayoutGuide.heightAnchor).
-
6:29
Okay, let's see what this looks like, Oops,
-
6:34
this is constraint equalTo, not constraint equalToConstant.
-
6:38
Okay, so to run this, we're going to go back to Main.storyboard,
-
6:44
and we'll set this backing class on
-
6:47
this default seen here to LayoutGuidesController, and
-
6:52
we'll run the app, And now you can see the end result.
-
6:58
So here we have three views that are equally spaced in the main view.
-
7:02
Now the benefit of this approach,
-
7:04
since the heights of the layout guides are determined by just dividing the available
-
7:07
space, is that we're not dependent on knowing the heights of any of the views,
-
7:12
nor do the views have to be of equal heights.
-
7:15
You can modify the height of either one of these views,
-
7:18
and we'll still get equal spacing above and below.
-
7:21
In addition, this layout can easily adapt to different device sizes and
-
7:25
orientations.
-
7:27
So to summarize, in Interface Builder you have access to some
-
7:30
predefined layout guides that you can use, like the safe area layout guides or
-
7:35
the previously named top and bottom layout guides.
-
7:37
But in code layout guides are much more flexible.
-
7:40
You can define any number of layout guides and
-
7:43
use them as dummy views to position your real views.
-
7:46
You cannot do this in Interface Builder.
You need to sign up for Treehouse in order to download course files.
Sign up