Overloads
2 ways to call compose().contentShape(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().contentShape(
kind: SwiftUI.ContentShapeKinds,
shape: SwiftUI.Shape,
options?: {
eoFill?: boolean;
}
)nonisolated func contentShape<S>(
_ kind: ContentShapeKinds,
_ shape: S,
eoFill: Bool = false
) -> some View
where S : ShapeParameters
The kinds to apply to this content shape.
shape*SwiftUI.ShapeThe shape to use.
options.eoFillbooleanA Boolean that indicates whether the shape is interpreted with the even-odd winding number rule.
Return value
A view that uses the given shape for the specified kind.
Discussion
The content shape has a variety of uses. You can control the kind of the
content shape by specifying one in kind. The following example sets
the focus ring shape of the view, without affecting its shape for
hit-testing:
MyFocusableView()
.contentShape(.focusEffect, Circle())You can apply multiple kinds of content shapes to the same view. For
example, apply a ContentShapeKinds.interaction shape and
ContentShapeKinds.focusEffect shape together to set both the
hit-testing shape and focus ring shape on a view.
Context Menu & Drag Previews
You can control the preview shown by the system for context menus or
drags using the relevant content shape kind, like
ContentShapeKinds.dragPreview and
ContentShapeKinds.contextMenuPreview.
The following example creates a VStack of an Image and Text
that has a context menu with a custom content shape:
VStack {
Image("turtlerock")
.contentShape(.contextMenuPreview,
RoundedRectangle(cornerRadius: 10))
Text("Turtle Rock")
}
.contextMenu {
Button {
// Add this item to a list of favorites.
} label: {
Label("Add to Favorites", systemImage: "heart")
}
}When someone activates the context menu with an action like touch and
hold in iOS or iPadOS, the system uses the Image as the preview for
the context menu, applying the requested corner radius.
The content shape also supports applying modifiers such as
InsettableShape.inset(by:) to add padding.
the ContentShapeKinds.contextMenuPreview and
ContentShapeKinds.dragPreview content shapes do not impact the
hit-testing shape. In this example, someone can touch and hold anywhere
on the VStack to activate the menu. If you only want the Image
to activate the menu, apply View.contextMenu(menuItems:) to the
Image instead.
See also
More in Drawing and shapes.
- blendModeSets the blend mode for compositing this view with overlapping views.
- buttonBorderShapeSets the border shape for buttons in this view.
- clipShapeSets a clipping shape for this view.
- containerShapeSets the container shape to use for any container relative shape within this view.
- coordinateSpaceAssigns a name to the view's coordinate space, so other code can operate on dimensions like points and sizes relative to the named space.
- materialActiveAppearanceSets an explicit active appearance for materials in this view.
- textInputBorderShapeSets the border shape for text input controls in the view hierarchy.