Shape
The Shape
protocol
is used to create and implement built-in, and custom shapes.
It inherits from the protocols: Sendable
, Animatable
, and View
.
Instantiating a Shape
Some shapes conform to the Shape
protocol like circles and rectangles. To instantiate each of these built-in types, refer to Self
:
Circle()Rectangle()
Some other shapes include:
Creating Custom Shapes
Shapes that are not built-in can be defined using path operations and should conform to the Shape
protocol.
struct someShape: Shape {func path(in rect: CGRect) -> Path {// Define the shape using path operations}}
The function should return a Path
object that describes the shape contained inside a rectangular frame of reference.
Example: Custom Triangle Shape
To build a triangle shape, a custom one can be made using path operations.
import SwiftUIstruct Triangle: Shape {func path(in rect: CGRect) -> Path {Path { path inpath.move(to: CGPoint(x: rect.midX, y: rect.minY))path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))path.addLine(to: CGPoint(x: rect.midX, y: rect.minY))}}}
By conforming to the Shape
protocol, a custom shape inherits all the capabilities of a built-in shape.
Displaying the Custom Shape
To display the custom shape, it can be called in a layout view just like any built-in shape:
var body: some View {ZStack {// Sets the color of the backgroundColor.blue.ignoresSafeArea()Triangle().foregroundColor(.yellow).frame(width: 180, height: 180)}}
This code results in the following output:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn SwiftUI on Codecademy
- Skill path
Build iOS Apps with SwiftUI
Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.Includes 7 CoursesWith CertificateBeginner Friendly13 hours - Free course
Learn Swift
A powerful programming language developed by Apple for iOS, macOS, and more.Beginner Friendly12 hours