Updated for Xcode 16.4
SwiftUI has a number of built-in shapes that are self-explanatory, but there’s one that stands out: ContainerRelativeShape. This isn’t a fixed shape, but is instead designed to be an insettable version of whatever shape it’s placed inside, which is particularly important when creating home screen widgets.
Important: At this time, ContainerRelativeShape works only inside widgets. You can use it elsewhere, but it will just make a rectangle.
For example, we could write code that draws a blue shape in our widget, and use ContainerRelativeShape to make sure it’s the same shape as the widget itself:
struct ContentView: View { var body: some View { ZStack { ContainerRelativeShape() .inset(by: 4) .fill(.blue) Text("Hello, World!") .font(.title) } .frame(width: 300, height: 200) .background(.red) .clipShape(Capsule()) } } 
SwiftUI is smart here: as you inset the container shape further and further, it will scale the corner radius of the container so it looks great in your widget.
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further for less! Get my all-new book Everything but the Code to make more money with apps, get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn Swift Testing, design patterns, and more.