How to scale a view up or down
About 2 min
How to scale a view up or down êŽë š
SwiftUI by Example
Back to Home
How to scale a view up or down | SwiftUI by Example
How to scale a view up or down
Updated for Xcode 15
SwiftUIâs scaleEffect()
modifier lets us increase or decrease the size of a view freely.
For example, we could make a text view five times its regular size like this:
Text("Up we go")
.scaleEffect(5)
.frame(width: 300, height: 300)
You can scale the X and Y dimensions independently if you want, allowing you to squash views like this:
Text("Up we go")
.scaleEffect(x: 1, y: 5)
.frame(width: 300, height: 300)
If you want more control, you can specify an anchor for your scaling like this:
VStack {
Text("Up we go")
.scaleEffect(2, anchor: .bottomTrailing)
Text("Up we go")
}
That makes the text view twice its regular size, scaled from the bottom-right corner.
Tips
Scaling up a view wonât cause it to be redrawn at its new size, only stretched up or down. This means small text will look fuzzy, and small images might look pixellated or blurry.
Similar solutionsâŠ
SwiftUI tips and tricks | SwiftUI by Example
SwiftUI tips and tricks
How to convert a SwiftUI view to an image | SwiftUI by Example
How to convert a SwiftUI view to an image
How to make a view dismiss itself | SwiftUI by Example
How to make a view dismiss itself
How to create and compose custom views | SwiftUI by Example
How to create and compose custom views
What's the difference between @ObservedObject, @State, and @EnvironmentObject? | SwiftUI by Example
What's the difference between @ObservedObject, @State, and @EnvironmentObject?