Source Freeze

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

MBProgressHUD example in Swift

August 15, 2022 by Source Freeze Leave a Comment

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

Leave a Reply Cancel reply

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

Recent Posts

  • How to Generate random numbers in JavaScript
  • How to Scroll to an Element in a React Component?
  • How to get a Date without the Time in JavaScript
  • How to modify url without reloading the page using javascript
  • How to disable server side rendering in nextjs
  • How to get a file type from URL in JavaScript

Recent Posts

  • How to Generate random numbers in JavaScript
  • How to Scroll to an Element in a React Component?
  • How to get a Date without the Time in JavaScript
  • How to modify url without reloading the page using javascript
  • How to disable server side rendering in nextjs
  • How to get a file type from URL in JavaScript

Recent Comments

    Tags

    beginner colon cross-platform es6 function html ionic iOS javascript mobile application development nextjs objective-c swift switch ternary typescript uiwebview Visual Studio plugin web developer

    Copyright © 2025 Source Freeze