SwiftUI .scaledToFill()

jareddanieljones's avatar
Published May 1, 2023Updated May 15, 2024
Contribute to Docs

The .scaledToFill() modifier method scales a view to fill its parent view while maintaining the original aspect ratio. It takes no parameters.

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours

Syntax

View
    .scaledToFill()

Note: This method is the same as using .aspectRatio() with a nil aspect ratio and a content mode of .fill.

Example

In the example below, the .scaledToFill() modifier is applied to a circle:

import SwiftUI
struct MyStruct: View {
var body: some View {
Circle()
.scaledToFill()
}
}

The image on the left shows the circle without the .scaledToFill() modifier applied. The image on the right shows the circle with the .scaledToFill() modifier applied.

SwiftUI ViewModifier scaledToFill

All contributors

Contribute to Docs

Learn SwiftUI on Codecademy

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours