Instance Method

aspectRatio(_:contentMode:)

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

Declaration

func aspectRatio(_ aspectRatio: CGFloat? = nil, contentMode: ContentMode) -> some View

Parameters

aspectRatio

The ratio of width to height to use for the resulting view. If aspectRatio is nil, the resulting view maintains this view’s aspect ratio.

contentMode

A flag indicating whether this view should fit or fill the parent context.

Return Value

A view that constrains this view’s dimensions to aspectRatio, using contentMode as its scaling algorithm.

Discussion

If this view is resizable, the resulting view will have aspectRatio as its aspect ratio. In this example, the purple ellipse has a 3:4 width to height ratio, and scales to fit its frame:

Ellipse()
.fill(Color.purple)
.aspectRatio(0.75, contentMode: .fit)
.frame(width: 200, height: 200)
.border(Color(white: 0.75))

See Also

Scaling Views

func scaledToFill() -> View

Scales the view to fill its parent.

func scaledToFit() -> View

Scales the view to fit 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(CGSize, contentMode: ContentMode) -> View

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

func imageScale(Image.Scale) -> View

Sets the scale of images inside the view.