What is UIActivityIndicatorView?
UIActivityIndicatorView
is a built-in loading spinner in iOS that provides visual feedback when a task is in progress (e.g., loading data, making network requests). It is commonly used to indicate ongoing processes and improve the user experience.
1. How to Add UIActivityIndicatorView in Swift
A. Adding UIActivityIndicatorView Programmatically
import UIKit
class ViewController: UIViewController {
let activityIndicator = UIActivityIndicatorView(style: .large)
override func viewDidLoad() {
super.viewDidLoad()
// Set position
activityIndicator.center = view.center
// Set color
activityIndicator.color = .blue
// Hide when stopped
activityIndicator.hidesWhenStopped = true
// Add to view
view.addSubview(activityIndicator)
// Start loading
activityIndicator.startAnimating()
// Simulate a delay (e.g., network call)
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.activityIndicator.stopAnimating()
}
}
}
✅ Explanation:
- Creates a
UIActivityIndicatorView
. - Sets its color and hides it when stopped.
- Starts animating and stops after 3 seconds.
B. Adding UIActivityIndicatorView via Storyboard
- Open Main.storyboard in Xcode.
- Drag and drop a
UIActivityIndicatorView
onto yourViewController
. - Set the Style, Color, and Behavior in the Attributes Inspector.
- Create an IBOutlet in
ViewController.swift
:@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
- Start and stop animation when needed:
activityIndicator.startAnimating() // Show loading activityIndicator.stopAnimating() // Hide loading
2. Customizing UIActivityIndicatorView
A. Changing the Size
activityIndicator.style = .large // Use .medium for smaller size
B. Changing the Color
activityIndicator.color = .red
C. Hiding When Stopped
activityIndicator.hidesWhenStopped = true
3. Using UIActivityIndicatorView with Network Requests
import UIKit
class ViewController: UIViewController {
let activityIndicator = UIActivityIndicatorView(style: .large)
override func viewDidLoad() {
super.viewDidLoad()
// Configure and add activity indicator
activityIndicator.center = view.center
activityIndicator.color = .gray
activityIndicator.hidesWhenStopped = true
view.addSubview(activityIndicator)
// Simulate API call
loadData()
}
func loadData() {
activityIndicator.startAnimating() // Show loading
DispatchQueue.global(qos: .background).async {
// Simulating a network delay
sleep(3)
DispatchQueue.main.async {
self.activityIndicator.stopAnimating() // Hide loading
}
}
}
}
✅ Explanation:
- Starts animating before a network call.
- Runs the task on a background thread to avoid UI freezing.
- Stops animating after the task completes.
4. UIActivityIndicatorView in a Button Action
@IBAction func loadData(_ sender: UIButton) {
sender.isEnabled = false // Disable button
activityIndicator.startAnimating()
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.activityIndicator.stopAnimating()
sender.isEnabled = true // Re-enable button
}
}
✅ Explanation:
- Disables the button while loading.
- Re-enables the button after 3 seconds.
5. Using UIActivityIndicatorView in UITableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.isEmpty ? 1 : data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if data.isEmpty {
let activityIndicator = UIActivityIndicatorView(style: .medium)
activityIndicator.startAnimating()
cell.contentView.addSubview(activityIndicator)
activityIndicator.center = cell.contentView.center
} else {
cell.textLabel?.text = data[indexPath.row]
}
return cell
}
✅ Explanation:
- Shows a loading spinner in a table cell if data is empty.
- Displays regular table rows when data is available.
6. Summary of UIActivityIndicatorView Features
Feature | Code Example |
---|---|
Set size | activityIndicator.style = .large |
Change color | activityIndicator.color = .blue |
Hide when stopped | activityIndicator.hidesWhenStopped = true |
Start animation | activityIndicator.startAnimating() |
Stop animation | activityIndicator.stopAnimating() |
Use with API calls | DispatchQueue.global(qos: .background).async { sleep(3) } |
Conclusion
UIActivityIndicatorView
is an easy-to-use loading spinner that enhances user experience during waiting times. It can be used in buttons, table views, and network calls to indicate background processes.
Would you like a customized version with a progress bar or animation? 🚀