Skip to content
DeveloperMemos

Using SwiftUI's ProgressView

SwiftUI, ProgressView2 min read

As an iOS developer, it is essential to provide a smooth and responsive user interface. When performing long-running tasks, it is important to provide visual feedback to the user, letting them know that the task is still in progress. This is where the ProgressView comes in handy.

In this article, we'll learn how to use the ProgressView in SwiftUI and explore some use cases.

What is ProgressView?

The ProgressView is a view that provides visual feedback for long-running tasks. It displays a horizontal bar that fills up as the task progresses. The ProgressView can be customized to fit your app's design and can display text to indicate the progress percentage.

The ProgressView has two styles, DefaultProgressViewStyle and CircularProgressViewStyle. The DefaultProgressViewStyle is a horizontal bar that fills up as the task progresses. The CircularProgressViewStyle is a circular indeterminate progress indicator.

Using ProgressView

Using the ProgressView is straightforward. You can create a ProgressView with the ProgressView initializer, passing in a progress value between 0 and 1.

1struct ContentView: View {
2 @State private var progressValue = 0.5
3
4 var body: some View {
5 VStack {
6 ProgressView(value: progressValue)
7 .frame(width: 200, height: 10)
8
9 Button("Start Progress") {
10 startProgress()
11 }
12 }
13 }
14
15 func startProgress() {
16 progressValue = 0.0
17
18 withAnimation(.linear(duration: 10.0)) {
19 progressValue = 1.0
20 }
21 }
22}

In the above code, we have a ContentView with a ProgressView and a Button. When the button is tapped, it calls the startProgress function, which updates the progressValue property from 0.0 to 1.0 over a duration of 10 seconds.

Notice that we have wrapped the progressValue update in the withAnimation block. This tells SwiftUI to animate the change, giving the ProgressView a smooth and responsive feel.

Customizing ProgressView

The ProgressView can be customized to fit your app's design. You can set the style of the ProgressView using the progressViewStyle modifier.

1ProgressView()
2 .progressViewStyle(CircularProgressViewStyle())

In the above code, we have set the progressViewStyle modifier to CircularProgressViewStyle(), which will display the ProgressView as a circular indeterminate progress bar.

You can also set the tint color of the ProgressView using the tint modifier.

1ProgressView()
2 .tint(.red)

In the above code, we have set the tint color of the ProgressView to red.

Use Cases

The ProgressView can be used in a variety of use cases. Here are a few examples:

  • Uploading or downloading files
  • Loading data from a remote server
  • Performing complex calculations
  • Animating an onboarding process

Wrap Up

In this article, we learned how to use the ProgressView in SwiftUI to provide visual feedback for long-running tasks. We explored how to create a basic ProgressView, customize it, and use it in a few different use cases. Using the ProgressView is an easy and effective way to provide feedback to your users while performing long-running tasks. By customizing the style and tint color, you can ensure that the ProgressView fits your app's design.