Constrains the view’s dimensions to the specified aspect ratio.
SDKs
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- Xcode 11.0+
Framework
- Swift
UI
Declaration
func aspectRatio(_ aspectRatio: CGFloat? = nil, contentMode: Content Mode) -> some View
Parameters
aspectRatio
The ratio of width to height to use for the resulting view. If
aspect
isRatio 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 aspect
, using content
as its scaling algorithm.
Discussion
If this view is resizable, the resulting view will have aspect
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))