Custom Circular Progress Bar in SwiftUI

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

Circular progress bars, also known as donut charts, are a common visual element used to display progress or completion percentage in a circular format. In SwiftUI, you can create a custom circular progress bar by combining different views and applying transformations to them.

Here is an example of how you could create a custom circular progress bar in SwiftUI:

struct CircularProgressBar: View {
    @Binding var progress: Double
    var size: CGFloat = 200

    var body: some View {
        ZStack {
            Circle()
                .stroke(Color.secondary, style: StrokeStyle(lineWidth: 10))
                .frame(width: size, height: size)

            Circle()
                .trim(from: 0, to: CGFloat(min(progress, 1)))
                .stroke(Color.primary, style: StrokeStyle(lineWidth: 10, lineCap: .round))
                .frame(width: size, height: size)
                .rotationEffect(Angle(degrees: -90))
                .animation(.linear)

            Text("\(Int(progress * 100))%")
                .font(.title)
                .foregroundColor(.primary)
        }
    }
}

This code creates a CircularProgressBar struct that conforms to the View protocol and has a @Binding variable called progress that is used to track the progress of the bar, and a size variable that determines the size of the bar.

The body of the CircularProgressBar consists of a ZStack with three views: a circle for the track, a circle for the progress, and a text label for the progress percentage.

The track circle is created using the Circle view and is styled with a secondary color and a line width of 10 points. The progress circle is created using the Circle view and is styled with a primary color, a line width of 10 points, and a line cap of .round. The trim modifier is used to trim the progress circle from 0 to the progress value, and the rotationEffect modifier is used to rotate the circle by -90 degrees so that it starts at the top. The animation modifier is used to animate the progress circle smoothly.

The text label is created using the Text view and displays the progress percentage as an integer.

To use the CircularProgressBar, you simply need to pass a binding to a Double variable as the progress argument and specify the size of the bar as the size argument. The CircularProgressBar will automatically update the progress and percentage based on the value of the progress variable.

You can customize the appearance and behavior of the CircularProgressBar by using different views and properties. For example, you could add a gradient fill to the progress circle, or use a different shape or color for the track or text label. You could also add a gesture or animation to the CircularProgressBar to make it more interactive.

struct ContentView: View {
    @State var progress = 0.2
    var body: some View {
        VStack {
            CircularProgressBar(progress: $progress)
        }
        .padding()
    }
}


✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn



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.

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