vimal prakash
vimal prakash

Reputation: 1499

How to use MBProgressHUD with swift

here is my code , but it showing the progress . is there any error in this code? please give some idea to fix this, or give some link related to this.

class Approval: UIViewController {

var hud: MBProgressHUD = MBProgressHUD()

override func viewDidLoad() {
    super.viewDidLoad()

    fetchData()
}
   func fetchData(){
      hud.show(true)
      // doing some http request
      dispatch_async(dispatch_get_main_queue()) {
         hud.hide(true)          
      }
   }

}

Upvotes: 46

Views: 56570

Answers (10)

Bharathi Devarasu
Bharathi Devarasu

Reputation: 8287

Use the below code to render MBProgressHUD and after some action completed hide it as described.

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, wesite loading. 
        
        dispatch_async(dispatch_get_main_queue())
            {
                spinnerActivity.hideAnimated(true);
        }
        
        
    }

For more information follow this tutorial: https://sourcefreeze.com/mbprogresshud-example-swift/

Upvotes: 2

Harjot Singh
Harjot Singh

Reputation: 6927

Create Extension to Easy to use and throughout application

extension UIViewController {

    func showHUD(progressLabel:String){
        DispatchQueue.main.async{
            let progressHUD = MBProgressHUD.showAdded(to: self.view, animated: true)
            progressHUD.label.text = progressLabel
        }
    }

    func dismissHUD(isAnimated:Bool) {
        DispatchQueue.main.async{
            MBProgressHUD.hide(for: self.view, animated: isAnimated)
        }
    }
}

USAGE:

1. SHOW - self.showHUD(progressLabel: "Loading...")

2. HIDE - self.dismissHUD(isAnimated: true)

Upvotes: 10

Janmenjaya
Janmenjaya

Reputation: 4163

Added to @EricDXS's answer,

The Swift 3 version is here

To show:

let loadingNotification = MBProgressHUD.showAdded(to: self.view, animated: true)
loadingNotification.mode = MBProgressHUDMode.indeterminate
loadingNotification.label.text = "Loading"

To dismiss:

loadingNotification.hide(animated: true)

Upvotes: 2

Krutagn Patel
Krutagn Patel

Reputation: 99

step 1 : Download "MBProgressHUD.h" and "MBProgressHUD.m" file and Add both file into your project. it will ask you to Bridging for Objective C files. if you already done bridging then it won't ask.

step 2 : In Bridging File import MBProgressHUD "import MBProgressHUD.h"

step 3 : use below code to show progress hud or hide hud.

for Show

DispatchQueue.main.async {
    MBProgressHUD.showAdded(to: self.view, animated: true)
}

for hide

DispatchQueue.main.async {
    MBProgressHUD.hide(for: self.view, animated: true)
}

Upvotes: 1

javimuu
javimuu

Reputation: 1859

Try it with Swift 3:

func showLoadingHUD(to_view: UIView) {
    let hud = MBProgressHUD.showAdded(to: to_view, animated: true)
    hud.label.text = "Loading..."
}

func hideLoadingHUD(for_view: UIView) {
    MBProgressHUD.hideAllHUDs(for: for_view, animated: true)
}

If Swift compiler warning: hideAllHUDs is deprecated. We should store references when using more than one HUD per view

Use:

func hideLoadingHUD(for_view: UIView) {
    MBProgressHUD.hide(for: for_view, animated: true)
}

Upvotes: 3

user3182143
user3182143

Reputation: 9589

Go through the below code

class ViewController: UIViewController, MBProgressHUDDelegate {
    var hud : MBProgressHUD = MBProgressHUD()
    func fetchData() {
        hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
        hud.mode = MBProgressHUDModeIndeterminate
        hud.labelText = "Loading"
    }
}

If you want to dismiss the HUD

MBProgressHUD.hideHUDForView(self.view, animated: true)

Upvotes: 3

EricDXS
EricDXS

Reputation: 882

Updated Answer:

let loadingNotification = MBProgressHUD.showAdded(to: view, animated: true)
loadingNotification.mode = MBProgressHUDMode.indeterminate
loadingNotification.label.text = "Loading"

To dismiss the ProgressHUD:

MBProgressHUD.hideAllHUDs(for: view, animated: true)

Upvotes: 81

cvb
cvb

Reputation: 1755

Swift 3 extensions

import Foundation
import MBProgressHUD
import QuartzCore

extension UITableViewController {
    func showHudForTable(_ message: String) {
        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
        hud.label.text = message
        hud.isUserInteractionEnabled = false
        hud.layer.zPosition = 2
        self.tableView.layer.zPosition = 1
    }
}

extension UIViewController {
    func showHud(_ message: String) {
        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
        hud.label.text = message
        hud.isUserInteractionEnabled = false
    }

    func hideHUD() {
        MBProgressHUD.hide(for: self.view, animated: true)
    }
}

// Use extensions

Upvotes: 11

vimal prakash
vimal prakash

Reputation: 1499

I solved it like this:

import UIKit

class Loader: NSObject {

    class func show(message:String = "Processing...", delegate: UIViewController) {
        var load : MBProgressHUD = MBProgressHUD()
        load = MBProgressHUD.showHUDAddedTo(delegate.view, animated: true)
        load.mode = MBProgressHUDMode.Indeterminate

        if message.length > 0 {
            load.labelText = message;
        }

        UIApplication.sharedApplication().networkActivityIndicatorVisible = true

    }

class func hide(delegate:UIViewController) {
    MBProgressHUD.hideHUDForView(delegate.view, animated: true)
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
    }
}

Upvotes: 1

user843337
user843337

Reputation:

You can also try this approach which will keep the other activity running in the background allowing the UI to remain responsive, providing users with a better experience. This is the intended/recommended approach for using the MBProgressHUD.

let progressHUD = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
progressHUD.labelText = "Loading..."

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)) {

    // ...Run some task in the background here...

    dispatch_async(dispatch_get_main_queue()) {
        progressHUD.hide(true)

        // ...Run something once we're done with the background task...
    }
}

Upvotes: 13

Related Questions