From the course: Complete Guide to SwiftUI
Scale modifiers
From the course: Complete Guide to SwiftUI
Scale modifiers
- [Instructor] We've looked at space, shape, and color for our layout. Next, we'll look at size and scale. The best way to scale items is to leave the work for Swift UI to do. Let's head over to HeaderView to start this. We used a version of this with a surf banner. Images generally stay within a frame, but you can expand them using the resizable modifier. So you'll see right here in the image that I've got a bunch of stuff here and I'm going to start by commenting out both of these with the Command + / and see what happens. And this is what the image actually looks like and it's the actual size of the image. So if you don't use resizable, you get a full image like this. Now I'm going to uncomment resizable here and you get a distorted image. The distorted image is resizable as trying to fit as much as it can in the available space with the safe area not being used. So these two areas you can see are still white, but I can control that with two modifiers. One is the scale to fit, which I just showed you 'cause that's what we had on earlier. The other one is scaled to fill and let me go put that one in first. And now that space that we have fills the image by making it as big as possible to fill in this space. And so now we've got just a zoomed in image of that surfing image that we had. Now I can go further with this and these two ends here can actually be part of my image by using ignoresSafeArea, as you can see right here. And now it goes past the notch and the whole iPhone here is all picture. So I can do it that way. Now I do want to point out you must have resizable first, and it must be there. If I commented out now, you see that it drops back down to its original size and will stay in its original size, even if you have scale to fit or scale to fill here. Okay, now I'm going to go ahead and comment out scale to fill, and I'm going to go back to scale to fit because that works the best in my particular case. You can see it's still not doing anything because resizable isn't there. But you do want to make sure resizable is always first right after the image. Otherwise, this won't work if I put scale to fit first and then resizable, it won't work. All right, let's do this one other place just to get something to look good. And that's going to be over in our pizza image in menu item view here. So we've got this lovely image, which actually works kind of nicely on this phone, but I'm going to go here into my image stack and before my quarter radius and shadow. So I'm going to go over here and I'm going to set the size of this, and so I'm going to make this resizable. And that down there is resizable and that makes it a much bigger pizza, at least on this phone and in this view. And then we're going to do a scaled to fit here. And now it's got its correct proportions and it's showing the whole pizza in that whole space. Now once I've done this, if you go back over to content view, it doesn't change much because of the way our proportions are, but just a slightly bigger pizza. And you notice the shadow isn't as in the way here. So it's now working pretty well in this context. If I go ahead and I rotate it, it'll go ahead and switch around. Let's do an orientation change here. You can see that it shrinks big time so that it can fit into the available space. One more thing I want to do here though is go over here and I want my image to be the same size if I'm doing a place markers. I'm going to copy this and I'm going to place it down here in my surfboard image as well, so that that behaves itself as well. So if I do have a surfboard, it's going to be that same size as well for whatever kind of picture I have. Now, these scale modifiers adapt to different devices and orientations making designed for devices much simpler.
Contents
-
-
-
-
-
(Locked)
Presenting views2m 19s
-
(Locked)
Space modifiers2m 54s
-
(Locked)
Shape modifiers3m 54s
-
(Locked)
Text modifiers8m
-
(Locked)
Color modifiers6m 4s
-
(Locked)
Basic backgrounds8m 29s
-
(Locked)
Materials and shadows7m 17s
-
Scale modifiers4m 42s
-
(Locked)
Size modifiers5m 25s
-
(Locked)
Clean up joined views6m 9s
-
(Locked)
Challenge: Clean up OrderView1m 28s
-
(Locked)
Solution: Clean up OrderView8m 36s
-
(Locked)
-
-
-
-
-
-
-
-
-
-
-
-
-