How to add an activity indicator in SwiftUI
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Creating intuitive and responsive user interfaces is essential in mobile app development. One crucial aspect is providing feedback to users during operations that may take time to complete, such as network requests or complex data processing. An effective way to communicate this is through an activity indicator or spinner. In this article, we will explore how to implement an activity indicator in SwiftUI, the modern declarative UI framework introduced by Apple.
Why Use an Activity Indicator?
Activity indicators are visual cues that inform users that a process is running. They prevent user frustration by acknowledging the system's response and can improve perceived performance. In SwiftUI, we can create activity indicators by wrapping UIKit components or by leveraging SwiftUI's built-in tools.
Implementing an Activity Indicator
Using ProgressView
SwiftUI provides a ProgressView that can be used as an activity indicator. The following example demonstrates how to incorporate this into an application.
Code Explanation
- State Management: We utilize the
@Stateproperty wrapper for tracking the loading state. When the button is pressed,isLoadingtoggles totrue, simulating the start of a network call. - Button Action: The button triggers a simulated operation using
DispatchQueue. After a delay of 2 seconds, it togglesisLoadingback tofalse. - Conditional Loading: The
ProgressViewappears only whenisLoadingistrue, creating a seamless indication of activity.
Customizing with UIKit
SwiftUI doesn’t currently allow the full customization of ProgressView beyond style and color. To achieve more customizations, you can wrap UIKit’s UIActivityIndicatorView.
UIKit Spinner Explanation
- UIViewRepresentable: Use
UIViewRepresentableto bridge UIKit views into SwiftUI. - makeUIView & updateUIView: Create and update the
UIActivityIndicatorViewwithin these functions. - Customization: Modify properties like
styleandcolorto fit your application's design.
Summary Table
Below is a summary of key approaches and their respective advantages.
| Approach | Description | Advantages |
Using ProgressView | Built-in SwiftUI component | Easy to implement, requires minimal code |
| UIKit Spinner | Custom activity indicator using UIViewRepresentable | Expanded customization options |
| Conditional Display | Use @State to control visibility | Dynamic feedback based on app state |
Additional Enhancements
- Animations: Consider adding subtle animations or transitions to improve the appearance when activity indicators appear or disappear.
- Dynamic Messages: Pair activity indicators with descriptive text to provide more context on the operation being performed.
- Testing: Test performance and smoothness of transitions under different network conditions to ensure a consistent user experience.
Incorporating an activity indicator in SwiftUI enhances user experience by providing immediate feedback during asynchronous tasks. Whether using built-in components or integrating UIKit, you can effectively convey system activity and improve the overall feel of your application.

