如何使用swift的MBProgressHUD

问题描述 投票:39回答:11

这是我的代码,但它显示了进度。这段代码有什么错误吗?请给出一些想法来解决这个问题,或者提供一些与此相关的链接。

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)          
      }
   }

}
ios iphone swift ios8
11个回答
76
投票

更新答案:

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

要解雇ProgressHUD:

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

0
投票

步骤1:下载“MBProgressHUD.h”和“MBProgressHUD.m”文件并将两个文件添加到项目中。它会要求您为目标C文件进行桥接。如果你已经做过桥接,那就不会问。

步骤2:在桥接文件导入MBProgressHUD“导入MBProgressHUD.h”

第3步:使用下面的代码显示进度hud或隐藏hud。

为了表演

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

为了隐藏

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

-1
投票

使用CocoaPods在podfile pod'MBProgressHUD'中添加pod,将MBProgressHUD安装到您的项目中。然后通过'pod install'安装Pod。

详细解释 - http://ios-support.blogspot.in/2017/04/how-to-use-mbprogresshud-progress-in.html


13
投票

您还可以尝试这种方法,以保持其他活动在后台运行,从而使UI保持响应,为用户提供更好的体验。这是使用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...
    }
}

10
投票

Swift 3扩展

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

6
投票

创建扩展以便于使用和整个应用程序

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)
        }
    }
}

用法:

1. SHOW - self.showHUD(progressLabel:“Loading ...”)

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


3
投票

请看下面的代码

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

如果你想解雇HUD

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

2
投票

使用下面的代码渲染MBProgressHUD,完成一些操作后,按照描述隐藏它。

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);
        }


    }

有关更多信息,请参阅本教程:http://sourcefreeze.com/mbprogresshud-example-swift/


2
投票

尝试使用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)
}

如果Swift编译警告:hideAllHUDs is deprecated. We should store references when using more than one HUD per view

使用:

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

2
投票

添加到@ EricDXS的答案,

Swift 3版本就在这里

显示:

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

解雇:

loadingNotification.hide(animated: true)

1
投票

我这样解决了:

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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.