Demystifying SwiftUI Shapes: Unraveling the Art of Shapes, Paths, Arcs, and Curves

Darshana Jaisingh
Swift Universe
Published in
4 min readJun 1, 2024

--

Photo by Shubham Dhage on Unsplash

Hey there, fellow developers and aspiring code wizards! 🌟

Welcome to the first article in our series on SwiftUI Shapes, Paths, Arcs, and Curves. Today, we kick off with a deep dive into SwiftUI Shapes. Whether you’re a seasoned developer or just starting with SwiftUI, this guide will provide you with clear, actionable insights to enhance your app’s design.

As an iOS developer, mastering SwiftUI shapes can significantly elevate your app’s user interface and user experience. Custom shapes allow you to create visually striking and unique elements, making your app stand out in a crowded marketplace. Whether you’re designing custom buttons, dynamic backgrounds, or intricate icons, understanding how to utilize SwiftUI’s powerful shape tools will give you the creative edge to build engaging and memorable applications.

Exploring SwiftUI’s Default Shapes

SwiftUI provides five built-in shapes that are commonly used in app development: Rectangle, Rounded Rectangle, Circle, Ellipse, and Capsule.

Each of these shapes has unique properties and can be easily customized. Let’s dive into each shape and see how to use them in your SwiftUI projects.

The following SwiftUI view demonstrates the use of predefined shapes with different colors and sizes.

struct BasicShapes: View {
var body: some View {
VStack(spacing: 15) {
Rectangle()
.fill(Color.red)
.frame(width: 200, height: 100)

RoundedRectangle(cornerSize: CGSize(width: 20, height: 20))
.fill(Color.orange)
.frame(width: 200, height: 100)

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

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

Circle()
.fill(Color.pink)
.frame(width: 100, height: 100)
}
}
}
Example Output: Basic Shapes

The above image shows the output for the above BasicShapes example.

Practical Exploration

In this section, we’ll dive into a real-world scenario that mirrors the challenges you might encounter while developing your app. We’ll understand how SwiftUI’s default shapes can serve as invaluable tools in your arsenal, enabling you to craft sophisticated user interface elements with ease.

Let’s delve into an example where we construct a user profile card view, complete with essential details such as the user’s name, status, description, and a follow button.

The output will look as shown in the image below:

Profile Card View

Let's have a look at the code to achieve the above output:

struct ProfileCardView: View {
var body: some View {
ModernProfileCard()
.padding()
}
}

struct ModernProfileCard: View {
var body: some View {
VStack(spacing: 15) {
// User Profile
ZStack {
Circle()
.fill(Color.gray)
.frame(width: 120, height: 120)


Image(systemName: "person.fill")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 75, height: 75)
.foregroundColor(.white)
}

// User Name
Text("John Doe")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.black)

// Status Indicator
Circle()
.fill(Color.green)
.frame(width: 20, height: 20)

// Divider
Rectangle()
.fill(Color.gray.opacity(0.5))
.frame(height: 1)

// Bio
Text("iOS Developer | Tech Enthusiast")
.font(.subheadline)
.foregroundColor(.gray)

// Follow Button
Capsule()
.fill(Color.blue)
.frame(height: 40)
.overlay(
Text("Follow")
.foregroundColor(.white)
.font(.headline)
)
}
.padding()
.background(Color.white)
.cornerRadius(20)
.shadow(color: Color.black.opacity(0.5), radius: 5, x: 0, y: 5)
}
}

#Preview {
ProfileCardView()
}

Breaking Down the Example

  1. User Profile:
    A circular placeholder represents the user’s profile picture, with an icon indicating a default user image. This creates a visually appealing focal point for the card.
  2. User Name:
    The user’s name is prominently displayed in a bold and elegant font, ensuring readability and emphasizing personal identity.
  3. Status Indicator:
    A small green circle serves as an indicator of the user’s online status, providing immediate context and interactivity.
  4. Divider:
    A subtle horizontal line separates different sections of the profile card, enhancing visual clarity and organization.
  5. Bio:
    A concise bio or description offers additional insights into the user’s background or interests, fostering personalization and connection.
  6. Follow Button:
    A stylish capsule-shaped button prompts users to take action, encouraging engagement and interaction with the user profile.

Conclusion

By harnessing SwiftUI’s basic shapes, we’ve crafted a stylish and functional profile card that seamlessly integrates essential user details with modern aesthetics. This example showcases the versatility of SwiftUI in creating visually captivating UI elements that enhance the user experience and engagement.

As you explore SwiftUI further, experiment with different shapes, styles, and layouts to create unique and impactful designs tailored to your app’s needs. Stay inspired and innovative as you continue your journey in app development with SwiftUI!

If you found this article helpful, consider giving it a clap 👏 to show your appreciation! Don’t forget to follow me for more programming insights.

If you’d like to further support my work, consider contributing via Buy Me a Coffee. Your generosity keeps the content coming! ☕️

Happy Coding!

Swift Universe: Your Guide to Apple Development

Thank you for reading until the end. Before you go:

  1. Clap & Follow: If you found this article helpful or insightful, please consider leaving a clap 👏 to show your appreciation and consider following the writer for more content like this.
  2. Share the Knowledge: Sharing is caring! If you think this article could benefit others in the Apple development community, don’t hesitate to share it with your friends and colleagues.
  3. Stay Updated: Stay tuned for more valuable insights, tutorials, and tips on all things Apple development. Don’t miss out on the latest updates and techniques to level up your skills.
  4. Join the Swift Universe: Become part of our growing community of Apple developers. Whether you’re a beginner or an experienced developer, there’s always something new to learn and explore in the Swift Universe.

Keep coding, keep learning, and let’s continue exploring the vast universe of Apple development together! 🌟

--

--

Darshana Jaisingh
Swift Universe

I'm on a journey to master iOS/macOS development. Let's code together, explore new ideas, and grow together in the world of Apple development!