Positions the view within an invisible frame with the specified size.
SDKs
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
- Xcode 11.0+
Framework
- Swift
UI
Declaration
Parameters
width
A fixed width for the resulting view. If
width
isnil
, the resulting view assumes this view’s sizing behavior.height
A fixed width for the resulting view. If
width
isnil
, the resulting view assumes this view’s sizing behavior.alignment
The alignment of this view inside the resulting view.
alignment
applies if this view is smaller than the size given by the resulting frame.
Return Value
A view with fixed dimensions of width
and height
, for the parameters that are non-nil
.
Discussion
Use this method to specify a fixed size for a view’s width, height, or both. If you only specify one of the dimensions, the resulting view assumes this view’s sizing behavior in the other dimension.
For example, the first ellipse in the following code is rendered in a fixed 200 by 100 frame. The second ellipse has only its height fixed, at 100; its width still expands to fill its parent’s dimensions.
VStack {
Ellipse()
.fill(Color.purple)
.frame(width: 100, height: 100)
Ellipse()
.fill(Color.blue)
.frame(height: 100)
}
If this view is smaller than the resulting frame in either dimension, alignment
specifies this view’s alignment within the frame.
Text("Hello world!")
.frame(width: 200, height: 200, alignment: .topLeading)
.border(Color.gray)