Declaration
compose().layoutValue({
key: string,
value: unknown,
})nonisolated func layoutValue<K>(
key: K.Type,
value: K.Value
) -> some View
where K : LayoutValueKeyParameters
options.key*stringThe type of the key that you want to set a value for. Create the key as a type that conforms to the
LayoutValueKeyprotocol.options.value*unknownThe value to assign to the key for this view. The value must be of the type that you establish for the key's associated value when you implement the key's
LayoutValueKey.defaultValueproperty.
Return value
A view that has the specified value for the specified key.
Discussion
Use this method to set a value for a custom property that
you define with LayoutValueKey. For example, if you define
a Flexibility key, you can set the key on a Text view
using the key's type and a value:
Text("Another View")
.layoutValue(key: Flexibility.self, value: 3)For convenience, you might define a method that does this in an
extension to View:
extension View {
func layoutFlexibility(_ value: CGFloat?) -> some View {
layoutValue(key: Flexibility.self, value: value)
}
}This method makes the call site easier to read:
Text("Another View")
.layoutFlexibility(3)If you perform layout operations in a type that conforms to the
Layout protocol, you can read the key's associated value for
each subview of your custom layout type. Do this by indexing the
subview's proxy with the key. For more information, see
LayoutValueKey.
See also
More in Layout.
- alignmentGuideSets the view's vertical alignment.
- aspectRatioConstrains this view's dimensions to the specified aspect ratio.
- 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.