how do I make a rounded rectangle in swiftui

Generated on 10/24/2024

1 search

To create a rounded rectangle in SwiftUI, you can use the RoundedRectangle shape. Here's a simple example of how you can create a rounded rectangle with a specific corner radius:

import SwiftUI

struct ContentView: View {
    var body: some View {
        RoundedRectangle(cornerRadius: 25)
            .fill(Color.blue)
            .frame(width: 200, height: 100)
    }
}

In this example, RoundedRectangle is used to create a rectangle with rounded corners. The cornerRadius parameter specifies the radius of the corners. The fill modifier is used to set the color of the rectangle, and the frame modifier sets its size.

For more advanced customization, you can explore SwiftUI's Path and Shape protocols, which allow for more complex shapes and designs. If you're interested in learning more about drawing paths and shapes in SwiftUI, you might want to check out the session on building a spatial drawing app with RealityKit, which discusses using SwiftUI paths for flexible shape definitions.