Drawing paths and custom shapes in SwiftUI

Uplift iOS Interview

"Uplift iOS Interview" is a comprehensive guide to help aspiring iOS developers soar to new heights in their careers. This book is an indispensable tool for anyone looking to crack the iOS interview and impress their future employers with their technical prowess. With in-depth coverage of Swift, AutoLayout, SwiftUI, Multithreading, Memory management so on and so forth, this book is a treasure trove of knowledge for anyone looking to uplift their iOS development career.

To draw paths and custom shapes in SwiftUI, you can use the Path and Shape types provided by the SwiftUI framework.

The Path type represents a geometric path in two-dimensional space, and it consists of a sequence of straight and curved line segments. You can create a Path by constructing a Path object and adding line segments and curves to it using methods such as move(to:), addLine(to:), and addCurve(to:control1:control2:).

The Shape type represents a geometric shape that can be drawn within a rectangular frame, and it is defined by a closure that takes a Path object and modifies it to create the desired shape. You can create a Shape by defining a closure that adds line segments and curves to a Path object and returning the Path object from the closure.

Here’s an example of how to create a custom shape in SwiftUI by defining a closure that modifies a Path object:

struct Triangle: Shape {
  func path(in rect: CGRect) -> Path {
    var path = Path()

    path.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))

    return path

In this example, the Triangle struct conforms to the Shape protocol and defines a path(in:) method that takes a CGRect object and returns a Path object. The path(in:) method creates a Path object and adds line segments to it to create a triangle shape.

To use the Triangle shape, you can create an instance of the Triangle struct and use it as the shape parameter of a Shape view. For example:

  .frame(width: 200, height: 200)

This will create a red triangle shape with a size of 200×200 points. You can customize the appearance of the shape further by using additional modifiers, such as stroke() and lineWidth(_:), to control the stroke and line width of the shape.

Rev Up Your iOS Skills: Take a Dynamic Learning Journey
iOS Career Boost

iOS Career Boost is the ultimate learning journey to elevate your iOS development career through a dynamic blend of visual learning, handy cheat sheets, coding practice materials, and expertly curated tips and tricks

Get Ready to Shine: Mastering the iOS Interview

Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book. Your email address will only be used for the purpose of sending the newsletter and will not be shared with third parties or advertisers. Rest assured that we value your privacy and will not spam your inbox.

Connect with me on

Twitter and LinkedIn and don't hesitate to reach out with any questions about this post. Thank you for reading.

If you know someone who would benefit from reading this article, please share it with them.