Source Freeze

  • Home
  • iOS
  • Swift
  • Cross-Platform
  • About
  • Contact
Home » UIActivityindicatorview example in ios using swift

UIActivityindicatorview example in ios using swift

August 19, 2022 by Source Freeze 2 Comments

In this example we will how to use UIActivityIndicatorView Example in swift, mostly we have used an activity indicator to show some operation status like webview start and stop.

Related: Webview Example in swift  & MBProgressHUD Example

UIActivityindicatorview is appears as spinning wheel either it’s spinning or stopped state mostly an activity indicator in ios is used to show the task progress status.

This ios programming “UIActivityIndicator Example” uses swift so you must need Xcode 6 or greater version to run this application.

Open the Xcode and create a new single view application,

Then, enter the product name as “UIActivityIndicator Example” next fill out the Organization Name and Organization identifier then select “Swift” language and device family as iPhone and remains core data field unchecked because we are not using that feature. To create an activity indicator in ios we can either use a storyboard or programmatically create an instance let’s start our tutorial by using storyboard in the last part we will see how to create an activity indicator programmatically.

UIActivityIndicatorView Example

Adding UIActivityIndicator into storyboard

Then select the ViewController in the Main.storyboard file, drag and drop the UIActivityIndicator in the viewcontroller and also add start and stop button as mentioned in the below image.

UIActivityIndicatorView ViewController

Next, we need to create IBOutlet for the UIActivityIndicatorView and IBAction for start and stop UIButton. Create IBOutlet for UIActivityIndicatorView and name it as “activityIndicator” and create IBAction for start button as “startActivityIndicator” and stop button as “stopActivityIndicator”

Then go to the ViewController.swift  by selecting the Assistance editor and create IBOutlet for UIActivityIndicatorView

create activity indicator outlet

Select start activity indicator button and create action like below

start activity indicator action
Select stop activity indicator button and create action like below.

stop activity indicator action

startAnimating() – used to start activity indicator

stopAnimating() – used stop activity indicator

Add this below codes snippet into “startActivityIndicator” method in the ViewController file.

@IBAction func startActivityIndicator(sender: AnyObject) {        
activityIndicator.startAnimating()
}

Add the below code into “stopActivityindicator” method in the ViewController.swift file.

@IBAction func stopActivityIndicator(sender: AnyObject) {        
activityIndicator.stopAnimating()
}

Also you can automatically hide an activity indicator when the animation stops by set the hidesWhenStopped property to YES in the viewDidLoad()

Also able to configure the activity indicator appearance by changing the activityIndicatorViewStyle method and using center method can set activity indicator into the center of the view.

Add the below code in the viewDidLoad method.

override func viewDidLoad() {
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
activityIndicator.center = view.center
super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib.
}

Then Build and Run the UIActivityIndicatorView Example you will get the output like below.

 UIActivityIndicatorView Example Output

Create UIActivityIndicatorView programmatically

You can also Create UIActivityIndicatorView programmatically instead of using storyboard, while creating object use below code and follow same stop for start and stop activity indicator, then build and run the application you can will get the same output.

var activityIndicator = UIActivityIndicatorView(
activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
override func viewDidLoad() {
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
activityIndicator.center = view.center
super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib.
}

Filed Under: ios-tutorial, swift

Comments

  1. Appels+Oranjes says

    November 5, 2016 at 1:46 pm

    In the code example, the member supposedly should be weak and you forgot to add it as a subview

    Reply

Trackbacks

  1. MBProgressHUD example in Swift - Source Freeze says:
    June 13, 2016 at 7:33 pm

    […] the previous example we have learned how to create UIActivityIndicator Example in swift, this tutorial we will learn about how to create MBProgressHUD […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • iOS UIPickerView Example using Swift
  • UITableView tutorial using Swift in iOS
  • UIActivityindicatorview example in ios using swift
  • UIStepper Example in Swift
  • MBProgressHUD example in Swift
  • UIWebView example using swift in ios

TAGS

cross-platform ionic iOS javascript mobile application development objective-c swift uiwebview Visual Studio plugin

Categories

  • cordova
  • cross-platform
  • Hybrid
  • Ionic 2
  • ionic framework
  • ios-tutorial
  • javascript
  • Mobile App Development
  • phonegap
  • react-native
  • swift
  • UIAlertController
  • uidatepicker
  • uipickerview
  • uislider
  • UISwitch
  • uitableview
  • uitextfield
  • uiwebview
  • visual studio – plugin
  • WKWebView

Recent Posts

  • iOS UIPickerView Example using Swift
  • UITableView tutorial using Swift in iOS
  • UIActivityindicatorview example in ios using swift
  • UIStepper Example in Swift
  • MBProgressHUD example in Swift
  • UIWebView example using swift in ios

Recent Comments

  • zulfi on iOS UIPickerView Example using Swift
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework

Tags

cross-platform ionic iOS javascript mobile application development objective-c swift uiwebview Visual Studio plugin

Copyright © 2022 Source Freeze