UIActivityIndicatorView

UIActivityIndicatorView displays a gear to show a task is in progress.

Example

UIActivityIndicatorView

Instructions

Create

  1. Open Xcode
  2. Click File
  3. Click New
  4. Click Project...
  5. Under iOS, click Application
  6. Click Single View Application
  7. Click Next
  8. After Product Name, type your product name
  9. After Organization Name, type your organization name or leave blank
  10. After Organization Identifier, type your organization identifier
  11. After Language, select Swift
  12. After Devices, select Universal
  13. Uncheck Use Core Data
  14. Click Next
  15. Select a folder
  16. After Source Control, uncheck Create Git repository on
  17. Click Create

Edit ViewController.swift

  1. Open ViewController.swift
  2. Copy and paste code

Edit Main.storyboard

  1. Open Main.storyboard
  2. Click View
  3. Click Utilities
  4. Click Show Object Library
  5. Drag Activity Indicator View to View
  6. Click View
  7. Click Utilities
  8. Click Show Connections Inspector
  9. Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
  10. Click interfaceBuilderActivityIndicatorView

Run

  1. Click Product
  2. Click Run

Code

import UIKit

class ViewController: UIViewController
{
  @IBOutlet weak var interfaceBuilderActivityIndicatorView: UIActivityIndicatorView!
  
  override func prefersStatusBarHidden() -> Bool
  {
    return true
  }
  override func viewDidLoad()
  {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.lightGrayColor()
    
    self.interfaceBuilderActivityIndicatorView.startAnimating()
    self.view.removeConstraints(self.view.constraints())

    let whiteLargeActivityIndicatorView = UIActivityIndicatorView()
    whiteLargeActivityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    whiteLargeActivityIndicatorView.setTranslatesAutoresizingMaskIntoConstraints(false)
    whiteLargeActivityIndicatorView.startAnimating()
    self.view.addSubview(whiteLargeActivityIndicatorView)
    self.view.addConstraint(NSLayoutConstraint(item: whiteLargeActivityIndicatorView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderActivityIndicatorView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let whiteActivityIndicatorView = UIActivityIndicatorView()
    whiteActivityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.White
    whiteActivityIndicatorView.setTranslatesAutoresizingMaskIntoConstraints(false)
    whiteActivityIndicatorView.startAnimating()
    self.view.addSubview(whiteActivityIndicatorView)
    self.view.addConstraint(NSLayoutConstraint(item: whiteActivityIndicatorView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderActivityIndicatorView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: whiteActivityIndicatorView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: whiteLargeActivityIndicatorView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
    
    let grayActivityIndicatorView = UIActivityIndicatorView()
    grayActivityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
    grayActivityIndicatorView.setTranslatesAutoresizingMaskIntoConstraints(false)
    grayActivityIndicatorView.startAnimating()
    self.view.addSubview(grayActivityIndicatorView)
    self.view.addConstraint(NSLayoutConstraint(item: grayActivityIndicatorView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderActivityIndicatorView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: grayActivityIndicatorView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: whiteActivityIndicatorView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
    
    let colorLargeActivityIndicatorView = UIActivityIndicatorView()
    colorLargeActivityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    colorLargeActivityIndicatorView.color = UIColor.redColor()
    colorLargeActivityIndicatorView.setTranslatesAutoresizingMaskIntoConstraints(false)
    colorLargeActivityIndicatorView.startAnimating()
    self.view.addSubview(colorLargeActivityIndicatorView)
    self.view.addConstraint(NSLayoutConstraint(item: colorLargeActivityIndicatorView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: whiteLargeActivityIndicatorView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    let colorActivityIndicatorView = UIActivityIndicatorView()
    colorActivityIndicatorView.color = UIColor.redColor()
    colorActivityIndicatorView.setTranslatesAutoresizingMaskIntoConstraints(false)
    colorActivityIndicatorView.startAnimating()
    self.view.addSubview(colorActivityIndicatorView)
    self.view.addConstraint(NSLayoutConstraint(item: colorActivityIndicatorView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: whiteLargeActivityIndicatorView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: colorActivityIndicatorView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: colorLargeActivityIndicatorView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
    
    let startAnimatingLargeActivityIndicatorView = UIActivityIndicatorView()
    startAnimatingLargeActivityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    startAnimatingLargeActivityIndicatorView.setTranslatesAutoresizingMaskIntoConstraints(false)
    startAnimatingLargeActivityIndicatorView.startAnimating()
    self.view.addSubview(startAnimatingLargeActivityIndicatorView)
    self.view.addConstraint(NSLayoutConstraint(item: startAnimatingLargeActivityIndicatorView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: colorLargeActivityIndicatorView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    let startAnimatingActivityIndicatorView = UIActivityIndicatorView()
    startAnimatingActivityIndicatorView.setTranslatesAutoresizingMaskIntoConstraints(false)
    startAnimatingActivityIndicatorView.startAnimating()
    self.view.addSubview(startAnimatingActivityIndicatorView)
    self.view.addConstraint(NSLayoutConstraint(item: startAnimatingActivityIndicatorView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: colorLargeActivityIndicatorView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: startAnimatingActivityIndicatorView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: startAnimatingLargeActivityIndicatorView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
    
    let stopAnimatingLargeActivityIndicatorView = UIActivityIndicatorView()
    stopAnimatingLargeActivityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    stopAnimatingLargeActivityIndicatorView.setTranslatesAutoresizingMaskIntoConstraints(false)
    stopAnimatingLargeActivityIndicatorView.stopAnimating()
    self.view.addSubview(stopAnimatingLargeActivityIndicatorView)
    self.view.addConstraint(NSLayoutConstraint(item: stopAnimatingLargeActivityIndicatorView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: startAnimatingLargeActivityIndicatorView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    let stopAnimatingActivityIndicatorView = UIActivityIndicatorView()
    stopAnimatingActivityIndicatorView.setTranslatesAutoresizingMaskIntoConstraints(false)
    stopAnimatingActivityIndicatorView.stopAnimating()
    self.view.addSubview(stopAnimatingActivityIndicatorView)
    self.view.addConstraint(NSLayoutConstraint(item: stopAnimatingActivityIndicatorView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: startAnimatingLargeActivityIndicatorView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: stopAnimatingActivityIndicatorView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: stopAnimatingLargeActivityIndicatorView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
    
    let hidesWhenStoppedLargeActivityIndicatorView = UIActivityIndicatorView()
    hidesWhenStoppedLargeActivityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    hidesWhenStoppedLargeActivityIndicatorView.hidesWhenStopped = false
    hidesWhenStoppedLargeActivityIndicatorView.setTranslatesAutoresizingMaskIntoConstraints(false)
    hidesWhenStoppedLargeActivityIndicatorView.stopAnimating()
    self.view.addSubview(hidesWhenStoppedLargeActivityIndicatorView)
    self.view.addConstraint(NSLayoutConstraint(item: hidesWhenStoppedLargeActivityIndicatorView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: stopAnimatingLargeActivityIndicatorView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    let hidesWhenStoppedActivityIndicatorView = UIActivityIndicatorView()

    hidesWhenStoppedActivityIndicatorView.hidesWhenStopped = false
    hidesWhenStoppedActivityIndicatorView.setTranslatesAutoresizingMaskIntoConstraints(false)
    hidesWhenStoppedActivityIndicatorView.stopAnimating()
    self.view.addSubview(hidesWhenStoppedActivityIndicatorView)
    self.view.addConstraint(NSLayoutConstraint(item: hidesWhenStoppedActivityIndicatorView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: stopAnimatingLargeActivityIndicatorView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: hidesWhenStoppedActivityIndicatorView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: hidesWhenStoppedLargeActivityIndicatorView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
  }
}

External

Apple iOS Developer Library