Skip to main content

SwiftUI's built-in shapes

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

SwiftUI's built-in shapes 관련

SwiftUI by Example

Back to Home

SwiftUI's built-in shapes | SwiftUI by Example

SwiftUI's built-in shapes

Updated for Xcode 15

Improved in iOS 17

SwiftUI gives us five built-in shapes that are commonly used: rectangle, rounded rectangle, circle, ellipse, and capsule. The last three in particular are subtly different in how they behave based on what sizes you provide, but we can demonstrate all the options with a single example:

struct ContentView: View {
    var body: some View {
        ZStack {
            Rectangle()
                .fill(.gray)
                .frame(width: 200, height: 200)

            RoundedRectangle(cornerRadius: 25)
                .fill(.red)
                .frame(width: 200, height: 200)

            UnevenRoundedRectangle(cornerRadii: .init(topLeading: 50, topTrailing: 50))
                .fill(.orange)
                .frame(width: 200, height: 200)

            Capsule()
                .fill(.green)
                .frame(width: 100, height: 50)

            Ellipse()
                .fill(.blue)
                .frame(width: 100, height: 50)

            Circle()
                .fill(.white)
                .frame(width: 100, height: 50)
        }
    }
}

Download this as an Xcode projectopen in new window

That draws all five shapes: two at 200x200 and three at 100x50. However, because the drawing behavior of the shapes is different you’ll see all five shapes visible in the output:

  • Rectangle draws a box at the exact dimensions you specify.
  • RoundedRectangle does the same, except now you can round the corners by a certain amount. Its second parameter, style, determines whether you want classic rounded corners (.circular) or Apple’s slightly smoother alternative (.continuous). The default from iOS 13 to 16 was .circular, but this changes to .continuous from iOS 17 on.
  • UnevenRoundedRectangle is a rounded rectangle where only some corners are rounded. The default is 0 for any corner, but you can override as many as you want to get a custom effect.
  • Capsule draws a box where one edge axis is rounded fully, depending on whether the height or width is largest. Our shape is 100x50, so it will have rounded left and right edges while being straight on the top and bottom edges.
  • Ellipse draws an ellipse at the exact dimensions you specify.
  • Circle draws an ellipse where the height and width are equal, so when we provide 100x50 for the space we’ll actually get 50x50.

If you’re applying these shapes as clip shapes, content shapes, or similar, you can use the short-hand versions .capsule, ellipse, .rect(cornerRadius: 10), .rect(topLeadingRadius: 20, topTrailingRadius: 20), and so on.

Similar solutions…
How to combine shapes to create new shapes | SwiftUI by Example

How to combine shapes to create new shapes
How to fill and stroke shapes at the same time | SwiftUI by Example

How to fill and stroke shapes at the same time
How to display solid shapes | SwiftUI by Example

How to display solid shapes
How to draw polygons and stars | SwiftUI by Example

How to draw polygons and stars
How to draw a custom path | SwiftUI by Example

How to draw a custom path

이찬희 (MarkiiimarK)
Never Stop Learning.