Creating UIActivityIndicatorView Programmatically

In this tutorial, you will learn how to create an activity indicator in Swift programmatically. To do that, you will use the UIActivityIndicatorView interface element.

UIActivityIndicatorView is a user interface element in iOS that shows a spinning wheel, often used to indicate that a task is in progress. It’s a simple and effective way to give users feedback that some activity is happening, especially for tasks that might take some time, like network requests or complex calculations.

By the end of this tutorial, you will have a working Swift code example that you can use in your mobile application.

Step 1: Create UIActivityIndicatorView

To create a UIActivityIndicatorView programmatically in Swift, you first need to import the UIKit framework, which provides the necessary classes and interfaces for constructing and managing a graphical, event-driven user interface for your iOS application.

Then, you define a UIActivityIndicatorView instance within your view controller’s viewWillAppear method. This method is called before the view controller’s content view is added to the app’s view hierarchy.

import UIKit

class ViewController: UIViewController {
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // Create Activity Indicator
        let myActivityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.medium)
        
        // Additional setup will be done in the following steps
    }
}

Step 2: Position UIActivityIndicatorView

After creating the UIActivityIndicatorView, you can position it in the center of the main view. This is done by setting the center property of the activity indicator to the center property of the view controller’s main view. This ensures that the activity indicator appears in the middle of the screen, regardless of the screen’s size.

myActivityIndicator.center = view.center

Step 3: Set UIActivityIndicatorView Style

The UIActivityIndicatorView.Style style determines the appearance of the activity indicator. You can choose from several predefined styles, such as .medium.large, and .gray. Setting the style to .gray makes the activity indicator appear as a light gray spinning wheel.

myActivityIndicator.style = UIActivityIndicatorView.Style.gray

Step 4: Prevent Activity Indicator from Hiding When Stopped

By default, the activity indicator will automatically hide itself when it is stopped. If you want the activity indicator to remain visible even after it has stopped animating, you can set the hidesWhenStopped property to false. This might be useful in scenarios where you want to indicate that a process has been completed but still need to display the activity indicator for some reason.

myActivityIndicator.hidesWhenStopped = false

Complete Code Example

Combining all the steps above, here is the complete code example for creating a UIActivityIndicatorView programmatically, positioning it in the center of the main view, setting its style to gray, and preventing it from hiding when stopped.

import UIKit

class ViewController: UIViewController {
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // Create Activity Indicator
        let myActivityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.medium)
        
        // Position Activity Indicator in the center of the main view
        myActivityIndicator.center = view.center
        
        // Set UIActivityIndicatorView style to UIActivityIndicatorViewStyle.Gray
        myActivityIndicator.style = UIActivityIndicatorView.Style.gray
        
        // Prevent Activity Indicator from hiding when stopped
        myActivityIndicator.hidesWhenStopped = false
        
        // Start Activity Indicator
        myActivityIndicator.startAnimating()
        
        // Add Activity Indicator to the main view
        view.addSubview(myActivityIndicator)
    }
}

UIActivityIndicator-in-swift

Conclusion

I hope this tutorial was helpful for you. There are a lot more Swift code examples on this website if you check the  Swift Code Examples page.

Happy coding!