0
1
00:00:00,150 --> 00:00:08,910
Let's change our size class back to the default which is the iPhone XR and we want to fix our problem
1

2
00:00:09,000 --> 00:00:14,520
where our background image gets cut off when the orientation changes.
2

3
00:00:14,650 --> 00:00:17,890
So how can we do this?
3

4
00:00:17,940 --> 00:00:27,000
Well, essentially, we need this background to resize itself, so that all four edges of that image is tied
4

5
00:00:27,030 --> 00:00:36,360
to the four edges of our screen, so that the top edge here is always sticking 0 pixels to the top of whatever
5

6
00:00:36,600 --> 00:00:42,950
orientation the screens in. The right is always sticking 0 pixels to the right of the screen.
6

7
00:00:42,960 --> 00:00:50,130
The bottom is zero from the bottom and the left is zero from the left. But, of course, we can't just adjust
7

8
00:00:50,130 --> 00:00:51,190
our image like so
8

9
00:00:51,210 --> 00:00:56,470
because as soon as we flip back to the other orientation it stays exactly the same.
9

10
00:00:56,490 --> 00:01:05,250
So we need to add some rules or Constraints onto this image view. And that is what we're gonna tackle
10

11
00:01:05,280 --> 00:01:12,180
in this lesson. To begin, let's make sure that you also have the same size class selected, the iPhone XR,
11

12
00:01:12,240 --> 00:01:14,710
and that it's in portrait mode,
12

13
00:01:14,940 --> 00:01:21,840
and then we're going to select our AppBrewery Background image view, and we're going to resize it so
13

14
00:01:21,840 --> 00:01:29,730
that it fits the entire screen of our iPhone XR. And then we're going to make sure that our logo is
14

15
00:01:29,760 --> 00:01:32,490
bang in the middle of our screen.
15

16
00:01:32,550 --> 00:01:37,190
So this is kind of our desired look at least in portrait.
16

17
00:01:37,290 --> 00:01:43,350
From here, we're going to add some constraints to our AppBreweryBackground, so that even when it's in
17

18
00:01:43,350 --> 00:01:50,730
landscape or on a different device, it still is touching all four sides of the phone and it's stretching
18

19
00:01:50,970 --> 00:01:53,250
to fit the entire screen.
19

20
00:01:53,550 --> 00:01:56,610
And then we're gonna go and click on this button right here,
20

21
00:01:56,610 --> 00:02:04,140
the little square button which is going to allow us to add a Constraint. And the Constraint that we
21

22
00:02:04,140 --> 00:02:13,920
want to add is to specify a zero pixel distance from the top of the phone, zero from the left, zero from the
22

23
00:02:13,920 --> 00:02:16,000
right, and zero from the bottom.
23

24
00:02:16,050 --> 00:02:25,380
So we're going to click on these red lines and turn them from these dash lines to solid lines to activate
24

25
00:02:25,380 --> 00:02:30,180
those four Constraints, and then we're simply going to click on this button.
25

26
00:02:30,180 --> 00:02:37,620
Now, if you don't see zero in each of these boxes, then go back and make sure that your background is
26

27
00:02:37,680 --> 00:02:39,780
placed exactly like so,
27

28
00:02:39,840 --> 00:02:42,660
and it's a fitting the entire screen.
28

29
00:02:42,930 --> 00:02:46,770
Once you're done, go ahead and click Add 4 Constraints.
29

30
00:02:46,770 --> 00:02:50,920
And now we go into our landscape orientation.
30

31
00:02:51,090 --> 00:02:59,040
You can see that our background image view is now more or less stretching to fit the entire space. But
31

32
00:02:59,130 --> 00:03:00,540
there's a couple of problems here.
32

33
00:03:01,020 --> 00:03:07,890
Firstly, why is it that our background is not going all the way to the left and right? What's going on
33

34
00:03:07,890 --> 00:03:08,190
there?
34

35
00:03:08,820 --> 00:03:16,170
Well, we can actually explore our Constraints and figure out what's gone wrong with them by expanding
35

36
00:03:16,170 --> 00:03:24,270
our Constraints here and we can see what these constraints or rules are doing to our User Interface.
36

37
00:03:24,270 --> 00:03:33,180
So the first one is a rule that says the right side or the trailing side, as highlighted here, of our
37

38
00:03:33,180 --> 00:03:39,910
AppBreweryBackground should be equal to the safe areas trailing.
38

39
00:03:39,930 --> 00:03:48,840
So this means that when our phone goes into landscape, the safe area is the area which normally contains
39

40
00:03:48,930 --> 00:03:55,470
either the battery information or the signal information of the phone. And at the bottom of the iPhone
40

41
00:03:55,470 --> 00:04:01,440
XR, it's the home button essentially, it's the part which you swipe to go back to the home area.
41

42
00:04:02,010 --> 00:04:09,620
So often if we're creating an app that has buttons on it, then we don't want it to be in these safe areas.
42

43
00:04:09,660 --> 00:04:13,210
So we'll define rules that keeps it out of these areas.
43

44
00:04:13,410 --> 00:04:19,410
But in our case, because it's just a background, we want our background to actually cover the entire screen,
44

45
00:04:19,890 --> 00:04:23,080
rather than have this sort of blocky effect.
45

46
00:04:23,100 --> 00:04:27,330
So we actually don't want it to be set to the safe areas trailing
46

47
00:04:27,480 --> 00:04:31,170
and in fact, we want it to be set to the superview.
47

48
00:04:31,230 --> 00:04:39,180
So this is our current element. The superview is the view that contains this element. And the superview
48

49
00:04:39,210 --> 00:04:45,690
that's at the base of our View Controller is always going to be covering the entire screen.
49

50
00:04:45,750 --> 00:04:50,840
So we want to define this constraint. Rather than relative to the safe area,
50

51
00:04:50,940 --> 00:04:58,370
we're going to define it to the superview. So this one right here. And we're going to say that it should
51

52
00:04:58,370 --> 00:05:01,410
be equal to the superview's trailing.
52

53
00:05:01,880 --> 00:05:10,700
So now our AppBreweryBackground, the trailing, the right side edge, is now fitted to be zero pixels from the
53

54
00:05:11,030 --> 00:05:13,310
trailing edge of our view.
54

55
00:05:13,340 --> 00:05:16,640
So that's the one that's highlighted in blue right here.
55

56
00:05:16,760 --> 00:05:23,990
And if we now go into landscape, you can see it's now fitting right to the edge of that right side or
56

57
00:05:23,990 --> 00:05:25,590
the trailing side.
57

58
00:05:25,580 --> 00:05:29,390
So let's go ahead and fix the same thing with our leading view.
58

59
00:05:29,510 --> 00:05:35,690
So you can see we've got our AppBreweryBackground leading set to be equal to the safe area leading.
59

60
00:05:35,720 --> 00:05:41,000
So let's go ahead and change that from safe area to the superview.
60

61
00:05:41,000 --> 00:05:47,680
And now when we go into landscape, you can see that it looks perfect as it is. Now
61

62
00:05:47,810 --> 00:05:54,110
you can see that each of these rules when you click on them you can view them in more detail in the
62

63
00:05:54,110 --> 00:06:01,070
attribute pane. And you can see the rules that have been set up here that set the Constraint between
63

64
00:06:01,130 --> 00:06:05,930
a first item and a second item and the relationship between them.
64

65
00:06:06,410 --> 00:06:15,260
So for example, if I wanted my AppBreweryBackground app to stop at the safe area, then I would change this
65

66
00:06:15,590 --> 00:06:21,340
to my safe area and change that constant to zero.
66

67
00:06:21,410 --> 00:06:27,150
So the top of my image view is now zero from the top of my safe area.
67

68
00:06:27,240 --> 00:06:33,320
And if you want to see where the safe area actually is, you can simply click on it here, and you can see
68

69
00:06:33,380 --> 00:06:44,430
where it stops on the top and the bottom, and also in the landscape orientation as well.
69

70
00:06:44,810 --> 00:06:48,980
Let's restore that Constraint back to what it used to be
70

71
00:06:49,010 --> 00:06:53,350
which is where the top is equal to the top of the superview.
71

72
00:06:53,480 --> 00:06:54,890
And let's look at another thing.
72

73
00:06:55,010 --> 00:07:01,880
If we take a look at the trailing, so the one on the right, it's currently set to the superview's trailing,
73

74
00:07:02,450 --> 00:07:07,280
but we can also set it to be relative to the margin.
74

75
00:07:07,340 --> 00:07:13,250
So the margin is that thing that pops up when we have an element that we drag to the right or to the
75

76
00:07:13,250 --> 00:07:14,000
left.
76

77
00:07:14,000 --> 00:07:19,940
You can see that blue line that pops up. And often when you have a button or when you have a logo, you
77

78
00:07:19,940 --> 00:07:24,500
don't want it to be right to the edge because it's hard to read.
78

79
00:07:24,500 --> 00:07:28,390
And instead, you want it to have a little bit of a margin to the edges.
79

80
00:07:28,970 --> 00:07:37,900
So in that case, you can define this to be relative to the margin of the superview's trailing area.
80

81
00:07:37,940 --> 00:07:43,750
So if I select that, then change my constant to zero and hit enter,
81

82
00:07:43,790 --> 00:07:48,800
you can see it's now limiting my background to that margin.
82

83
00:07:48,800 --> 00:07:54,770
So these are some of the things that you can do with your Constraints. And when you add them using the
83

84
00:07:54,770 --> 00:07:59,750
menu here, they don't always get added the way that you think it will,
84

85
00:07:59,750 --> 00:08:05,540
you can have a little bit more control over it if you click on the dropdown list.
85

86
00:08:05,540 --> 00:08:14,510
So, for example, if I select my logo here and I click on here, then you can see that I can define it relative
86

87
00:08:14,510 --> 00:08:24,910
to the safe area or to the view, and I can change the distance or the value from those areas. So our background
87

88
00:08:25,030 --> 00:08:32,530
is now pretty much sorted. And if we take a look at all the different device sizes all the way from small
88

89
00:08:32,530 --> 00:08:40,140
ones to large ones, it's basically adapting it to fit the entire screen background.
89

90
00:08:40,390 --> 00:08:44,860
And even when we change the orientation, it still looks pretty good.
90

91
00:08:45,890 --> 00:08:53,360
The next thing to do is to figure out how we can layout our logo. And to do that, we need to learn about
91

92
00:08:53,630 --> 00:08:57,910
alignment. And that is what we're gonna do on the next lesson,
92

93
00:08:57,950 --> 00:08:58,910
so I'll see you there.
