NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SwiftUI’s built-in shapes

Paul Hudson    @twostraws   

Updated for Xcode 14.0 beta 1

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, style: .continuous)
                .fill(.red)
                .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 project

A gray rectangle in front of which is a rounded red rectangle with the same height and width. Inside of them is a green capsule in front of which is a blue ellipse with the same height and width as the capsule. Inside of them is a white circle with the same height as the ellipse.

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).
  • 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.
Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain. The code can be hard to write, full of edge cases, and time-consuming to maintain. RevenueCat provides a backend and wrapper around StoreKit to make things simple so you can focus on building your app.

Explore the docs

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.3/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.