如何使用swift的MBProgressHUD

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用swift的MBProgressHUD相关的知识,希望对你有一定的参考价值。

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

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

}
答案

更新答案:

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

要解雇ProgressHUD:

MBProgressHUD.hideAllHUDs(for: view, animated: true)
另一答案

步骤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)
}
另一答案

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

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

另一答案

您还可以尝试这种方法,以保持其他活动在后台运行,从而使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...
    }
}
另一答案

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
另一答案

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

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)

另一答案

请看下面的代码

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)
另一答案

使用下面的代码渲染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/

另一答案

尝试使用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)
}
另一答案

添加到@ EricDXS的答案,

Swift 3版本就在这里

显示:

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

解雇:

loadingNotification.hide(animated: true)
另一答案

我这样解决了:

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

以上是关于如何使用swift的MBProgressHUD的主要内容,如果未能解决你的问题,请参考以下文章

MBProgressHUD 导致 iOS 应用程序崩溃 (Swift 2.0)

如何快速使用 MBProgressHUD

在 iOS Swift 项目中找不到“UIView”的接口声明

如何在swift中使用cocoapods导入的第三方oc库

如何使用mbprogresshud

MBProgressHUD 崩溃“MBProgressHUD 初始化程序中使用的视图为零”