Overloads
2 ways to call compose().aspectRatio(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().aspectRatio(
aspectRatio: number | null | undefined,
options: {
contentMode: SwiftUI.ContentMode;
}
)nonisolated func aspectRatio(
_ aspectRatio: CGFloat? = nil,
contentMode: ContentMode
) -> some ViewParameters
aspectRatio*number | null | undefinedThe ratio of width to height to use for the resulting view. Use
nilto maintain the current aspect ratio in the resulting view.options.contentMode*SwiftUI.ContentModeA flag that indicates whether this view fits or fills the parent context.
Return value
A view that constrains this view's dimensions to the aspect ratio of the given size using contentMode as its scaling algorithm.
Discussion
Use aspectRatio(_:contentMode:) to constrain a view's dimensions to an
aspect ratio specified by a
doc://com.apple.documentation/documentation/CoreFoundation/CGFloat-swift.struct
using the specified content mode.
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
More in Layout.
- alignmentGuideSets the view's vertical alignment.
- containerCornerOffsetAdjusts the view's layout to avoid the container view's corner insets for the specified edges.
- containerRelativeFramePositions this view within an invisible frame with a size relative to the nearest container.
- controlSizeSets the size for controls within this view.
- defaultScrollAnchorAssociates an anchor to control which part of the scroll view's content should be rendered by default.
- dynamicTypeSizeSets the Dynamic Type size within the view to the given value.
- fixedSizeFixes this view at its ideal size.
- flipsForRightToLeftLayoutDirectionSets whether this view mirrors its contents horizontally when the layout direction is right-to-left.
- framePositions this view within an invisible frame.
- geometryGroupIsolates the geometry (e.g. position and size) of the view from its parent view.
- gridCellAnchorSpecifies a custom alignment anchor for a view that acts as a grid cell.
- gridCellColumnsTells a view that acts as a cell in a grid to span the specified number of columns.