Activity Indicator Tutorial in iOS8 with Swift

An Activity Indicator is a spinning wheel that indicates a task is being processed. if an action takes an unknown amount of time to process you should display an activity indicator to let the user know your app is not frozen. In this tutorial we will start and stop the spinning animation of the activity indicator.

Open Xcode and create a new Single View Application. For product name, use iOS8SwiftActivityIndicatorTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard. In Xcode 6 view controllers are now square by default, so that you can create one user interface use just for multiple devices. In iOS8 you can layout the storyboard using constraints and size classes. Since this app is only going to be for the iPhone, you can disable size classes. In the File Inspector untick Use Size Classes.

Choose Keep size class data for: iPhone, and click Disable Size Classes.

Add 2 buttons to the main view and Give them a title of Start and Stop. Next, drag a Activity Indicator just below the two buttons. Select the Activity Indicator and go to the Attributes inspector. In the Activity Indicator View section set the Style to Large White and change the Color to red. The red color will override the white color from the Style attribute. Also select the "Hides when stopped" option.

Open the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl + drag from the Activity Indicator to the ViewController class and create the following Outlet.

Ctrl + drag from the start button to the ViewController class and create the following Action.

Ctrl + drag from the stop button to the ViewController class and create the following Action.

Next, implement the two IBAction methods.

@IBAction func startSpinning(sender: UIButton) {

@IBAction func startSpinning(sender: UIButton) {

The methods will start and stop the animation of the spinning wheel. When the animation is stopped the activity indicator is automatically hidden from the view. Build and run the project and press the buttons to start and stop the animation.

You can download the source code of the iOS8SwiftActivityIndicatorTutorial at the ioscreator repository on github.