ConcentricRectangle
A shape whose corners you configure, individually or uniformly, to be squared, rounded, or concentric relative to a container shape's corners.
Declaration
type ConcentricRectangle =
| { animatableData?: ConcentricRectangleAnimatableData; }
| { corners: EdgeCornerStyle; isUniform?: boolean; }
| { topLeadingCorner?: EdgeCornerStyle; topTrailingCorner?: EdgeCornerStyle; bottomLeadingCorner?: EdgeCornerStyle; bottomTrailingCorner?: EdgeCornerStyle; }
| { uniformBottomCorners?: EdgeCornerStyle; topLeadingCorner?: EdgeCornerStyle; topTrailingCorner?: EdgeCornerStyle; }
| { uniformLeadingCorners?: EdgeCornerStyle; topTrailingCorner?: EdgeCornerStyle; bottomTrailingCorner?: EdgeCornerStyle; }
| { uniformLeadingCorners?: EdgeCornerStyle; uniformTrailingCorners?: EdgeCornerStyle; }
| { uniformTopCorners?: EdgeCornerStyle; bottomLeadingCorner?: EdgeCornerStyle; bottomTrailingCorner?: EdgeCornerStyle; }
| { uniformTopCorners?: EdgeCornerStyle; uniformBottomCorners?: EdgeCornerStyle; }
| { uniformTrailingCorners?: EdgeCornerStyle; topLeadingCorner?: EdgeCornerStyle; bottomLeadingCorner?: EdgeCornerStyle; };struct ConcentricRectangleType methods
Overview
Use ConcentricRectangle to create a rectangular shape that
fits inside a container's shape, similar to the way that a sheet's corners
in iOS match the curvature of the screen. System-provided elements like
sheets and popovers do this automatically. You can use this effect for
your custom views to match a device's curved edges, or for your custom
views near the edges inside another view with concentric corners. For
example, the Notes app format sheet has rounded bottom corners that are
concentric relative to the device's corners, and rounded top corners that
have a fixed radius.
@Image(source: "concentricrectangle-6", alt: "A screenshot of the Format sheet over the Notes app. The Format sheet has top corners that have rounded corners with a fixed radius, and rounded bottom corners that are concentric relative to the device's corners.", deviceFrame: "iPhone16-bottom-half")
A rounded corner of a rectangle is concentric relative to the container
shape's adjacent corner when the corner's radius shares a common
center with the containing shape's rounded corner radius. A containing
shape could be a view that extends to the device's rounded corners, or
any view that sets View.containerShape(_:)-(RoundedRectangularShape).
ConcentricRectangle automatically calculates each corner's radius
relative to the container shape, so your view adapts correctly across
devices and sizes without hard-coded values.
@Image(source: "concentricrectangle-3", alt: "A diagram of the bottom half of an iPhone with a gray background view. Over the gray background is a green view with uniformly rounded top corners, and uniformly rounded bottom corners that are concentric with the device's edge. Callouts identify the gray background as the container shape, the green view as a concentric rectangle, and a dot that's the common center of the radii of the bottom leading corner curves.")
Create a concentric rectangle
Create a ConcentricRectangle by specifying corner styles that reflect
the types of corners you want, with an initializer or static Shape
convenience method that specifies how to shape each corner. By default,
ConcentricRectangle's ConcentricRectangle.init() creates a shape
where each corner is individually concentric with the container shape:
@TabNavigator { @Tab("Code") {
```swift
ConcentricRectangle()
.fill(Color.green)
.padding(8.0)
.ignoresSafeArea()
.frame(height: 240.0)
```
}
@Tab("Preview") {
@Image(source: "concentricrectangle-4", alt: "A screenshot of a green view with squared top corners, and rounded bottom corners that are concentric with the device's edge.", deviceFrame: "iPhone16-bottom-half")
}}
When your ConcentricRectangle's corners are far away from the
containing shape's corners, such as the top corners in this example, the
corner radius the system calculates may be zero. When that happens, the
corner is square. It's also possible that your app is running on a device
whose corners are square. To ensure that your view always has rounded
corners that are concentric relative to the container shape when they can
be, use Edge.Corner.Style.concentric(minimum:) to specify a rounded
corner with a minimum radius.
SwiftUI provides container shapes by default in system-provided views.
To allow ConcentricRectangle to resolve corner radii based on
concentricity in your custom view, use
View.containerShape(_:)-(RoundedRectangularShape) to
specify a container shape that implements RoundedRectangularShape,
such as Circle, Rectangle, RoundedRectangle, or Capsule.
When the container shape does not conform to RoundedRectangularShape,
ConcentricRectangle provides an inset version of the container shape
like ContainerRelativeShape.
Customize corners
Select corner styles from the Edge.Corner.Style. enumeration to form
the following types of corners:
- A rounded corner with a radius that's concentric relative to the containing view
- A rounded corner with a radius that's concentric relative to the containing view, constrained with a minimum radius
- A rounded corner with a fixed radius
- A squared corner
The following example shows one concentric rectangle with each type of corner:
@TabNavigator { @Tab("Code") {
```swift
ConcentricRectangle(
topLeadingCorner: .concentric(minimum: 12.0),
topTrailingCorner: .fixed(24.0),
bottomLeadingCorner: .concentric,
bottomTrailingCorner: .fixed(0.0)
)
.fill(Color.green)
.padding(24.0)
.ignoresSafeArea()
.frame(height: 240.0)
```
}
@Tab("Preview") {
@Image(source: "concentricrectangle-5", alt: "A screenshot of a green view with a concentric top leading corner with a minimum radius, a fixed radius top trailing corner, a concentric bottom leading corner, and a square bottom trailing corner.", deviceFrame: "iPhone16-bottom-half")
}}
Create uniform corners
To create a shape similar to the Notes app format sheet, create a
ConcentricRectangle that specifies uniform top corners with a fixed
radius and concentric uniform bottom corners. The functions with uniform
corner styles calculate each uniform corner's radius first, then use the
largest radius for each uniform corner:
@TabNavigator { @Tab("Code") {
```swift
ConcentricRectangle(
uniformTopCorners: .fixed(24.0),
uniformBottomCorners: .concentric
)
.fill(Color.green)
.padding(8.0)
.ignoresSafeArea()
.frame(height: 240.0)
```
}
@Tab("Preview") {
@Image(source: "concentricrectangle-2", alt: "A screenshot of a green view with uniformly rounded top corners, and uniformly rounded bottom corners that are concentric with the device's edge.", deviceFrame: "iPhone16-bottom-half")
}}
Use initializers with uniform parameters to fit your concentric rectangle inside the containing view, depending on which corners need concentricity:
- All corners
- Leading corners only
- Trailing corners only
- Leading and trailing corners separately
- Top corners only
- Bottom corners only
- Top and bottom corners separately
From Apple’s SwiftUI documentation. Code samples there are Swift.
See also
More in Other values.
- AxisThe horizontal or vertical dimension in a 2D coordinate system.
- AxisSetAn efficient set of axes.
- ButtonRoleA value that describes the purpose of a button.
- CapsuleA capsule shape aligned inside the frame of the view containing it.
- CircleA circle centered on the frame of the view containing it.
- ContentMarginGuideA guide for resolving the content margins for a view.
- ContentMarginPlacementThe placement of margins.
- ContentModeConstants that define how a view's content fills the available space.
- DatePickerComponents
- DatePickerComponents2The date and time components that a date picker shows.
- DeviceHingeA type encapsulating the state of a single hinge.
- DeviceHingeContextA type describing the context of hinges on the device.