Instance Method

scaledToFit()

Scales this view to fit its parent.

Declaration

func scaledToFit() -> some View

Return Value

A view that scales this view to fit its parent, maintaining this view’s aspect ratio.

Discussion

Use scaledToFit() to scale this view to fit its parent, while maintaining the view’s aspect ratio as the view scales.

Circle()
    .fill(Color.pink)
    .scaledToFit()
    .frame(width: 300, height: 150)
    .border(Color(white: 0.75))
A screenshot of pink circle scaled to fit its frame.

This method is equivalent to calling aspectRatio(_:contentMode:) with a nil aspectRatio and a content mode of ContentMode.fit.

See Also

Scaling Views

func scaledToFill() -> View

Scales the view to fill its parent.

func scaleEffect(CGFloat, anchor: UnitPoint) -> View

Scales this view’s rendered output by the given amount in both the horizontal and vertical directions, relative to an anchor point.

func scaleEffect(CGSize, anchor: UnitPoint) -> View

Scales this view’s rendered output by the given vertical and horizontal size amounts, relative to an anchor point.

func scaleEffect(x: CGFloat, y: CGFloat, anchor: UnitPoint) -> View

Scales this view’s rendered output by the given horizontal and vertical amounts, relative to an anchor point.

func aspectRatio(CGFloat?, contentMode: ContentMode) -> View

Constrains the view’s dimensions to the specified aspect ratio.

func aspectRatio(CGSize, contentMode: ContentMode) -> View

Constrains the view’s dimensions to the aspect ratio of the specified size.

enum ContentMode

Constants that define how a view's content fills the available space.

func imageScale(Image.Scale) -> View

Scales images within the view according to one of the relative sizes available including small, medium, and large images sizes.