UIActivityIndicatorView
UIActivityIndicatorView displays a gear to show a task is in progress.
Example
Instructions
Create
- Open Xcode
- Click File
- Click New
- Click Project...
- Under iOS, click Application
- Click Single View Application
- Click Next
- After Product Name, type your product name
- After Organization Name, type your organization name or leave blank
- After Organization Identifier, type your organization identifier
- After Language, select Swift
- After Devices, select Universal
- Uncheck Use Core Data
- Click Next
- Select a folder
- After Source Control, uncheck Create Git repository on
- Click Create
Edit ViewController.swift
- Open ViewController.swift
- Copy and paste code
Edit Main.storyboard
- Open Main.storyboard
- Click View
- Click Utilities
- Click Show Object Library
- Drag Activity Indicator View to View
- Click View
- Click Utilities
- Click Show Connections Inspector
- Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
- Click interfaceBuilderActivityIndicatorView
Run
- Click Product
- 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)) } }