Source Freeze

  • Home
  • iOS
  • Swift
  • Cross-Platform
  • About
  • Contact
Home » MBProgressHUD example in Swift

MBProgressHUD example in Swift

August 15, 2022 by Source Freeze 2 Comments

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

What is MBProgressHUD?

MBProgressHUD is an custom control that is used to displays a translucent HUD with a progress indicator with some optional labels while work is being done in a background thread.

Comparing to default UIActivityIndicator, MBProgressHUD  is a very stable, flexible and very nice user interface implementation of an activity indicator with lot customization options and often many of us prefer to use this rather than default UIActivityIndicatorView.

it works on iOS 6+ and requires ARC to build.

Getting Started

Open the Xcode and create a new single view application,

Then, enter the product name as “MBProgressHUD Example” next fill out the Organization Name and Organization identifier then select “Swift” language and device family as iPhone and remains core data field as unchecked because we are not using that feature.

MBProgressHUD Example

Next, we need to configure our project with MBProgressHUD by referring it’s library files. this files are currently available in Objective-C, but still we can use that MBProgressHUD in swift project by following the below steps.

Download the MBPrgoressHUD Files using this link and extract the downloaded files, just drag and drop the below selected  source files into the project.

MBProgressHUD Library Files

While adding that project one prompt will open with the title of “Would like to configure an Objective-C bridging header?” please select “Create Bridging Header” button, it will create bridging header file to use  objective-c files in swift project.

MBProgressHUD Bridging Header

Open that created {productname}-bridging-header.h file and import MBProgressHUD.h file by adding the below code into bridging-header.h file,

#import "MBProgressHUD.h"

yes, the configuration part is over, now our project is ready to use with HUD library.

Open the Main.storyboard and select ViewController size as preferred iPhone size using Simulated Metrics option in the right side, then add two button from object library name it as “Start Loading” and “Stop Loading” as shown in the below image.

MBProgressHUD adding buttons

 Next have to create an IBAction for this two buttons by clicking show assistant editor.

Create startActivityIndicator function by adding IBAction for StartLoading Button,

IBAction for StartLoading Button

Create stopActivityIndicator function by adding IBAction for StopLoading Button,

IBAction for StopLoading Button

Then add the below code snippet in the startActivityIndicator method,

 
  @IBAction func startActivityIndicator(sender: AnyObject) 
  {         
  let spinnerActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true);         
  spinnerActivity.label.text = "Loading";         
  spinnerActivity.detailsLabel.text = "Please Wait!!";         
  spinnerActivity.userInteractionEnabled = false;     
  }
  

Here we are creating object for activity indicator, then adding values for label, detailsLabel. As well user can add other properties like color, background, setTimeOut etc..

Note:

We have set userInteractionEnabled Property as false it is allow to access other action while activity indicator is loading.

Next add the below code snippet in stopActivityIndicator function.


@IBAction func stopActivityIndicator(sender: AnyObject) 
{ 
  MBProgressHUD.hideAllHUDsForView(self.view, animated: true); 
}

Instead of this function we can also hide the MBProgressHUD by an already created reference object.

Then Build and Run the project will get the below output:

MBProgressHUD Example

Closing MBProgressHUD after specific action is completed:

MBProgressHUD is mainly used to show the specific action is occurs and once completed action we need to close after some set of actions is completed. For this scenario, we can use the below direct method to close automatically after specific actions are completed.

Please refer to the below code snippet for this.


let spinnerActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true); 
spinnerActivity.label.text = "Loading"; 
spinnerActivity.detailsLabel.text = "Please Wait!!"; 
spinnerActivity.userInteractionEnabled = false; 


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0))
{
// Add some background task like image download, website loading.
dispatch_async(dispatch_get_main_queue())
{
spinnerActivity.hideAnimated(true);
}
}

Filed Under: ios-tutorial, swift

Trackbacks

  1. UIActivityindicatorview example in ios using swift - Source Freeze says:
    August 3, 2016 at 7:38 pm

    […] Related: Webview Example in swift  & MBProgressHUD Example […]

    Reply
  2. UIWebView example using swift in ios - Source Freeze says:
    August 14, 2016 at 5:45 pm

    […] Related: https://sourcefreeze.com/mbprogresshud-example-swift/ […]

    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