Skip to main content

How to scale a view up or down

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

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)

Download this as an Xcode projectopen in new window

The large, slightly blurry text “Up we go”.
The large, slightly blurry text “Up we go”.

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)

Download this as an Xcode projectopen in new window

The text “Up we go” stretched vertically.
The text “Up we go” stretched vertically.

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")
}

Download this as an Xcode projectopen in new window

Two lines, both reading “Up we go”. The upper line is both larger and offset to the left such that the lines' trailing edges align.
Two lines, both reading “Up we go”. The upper line is both larger and offset to the left such that the lines' trailing edges align.

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?

이찬희 (MarkiiimarK)
Never Stop Learning.