如何使用swift的MBProgressHUD

vim*_*ash 43 iphone ios swift ios8

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

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

}
Run Code Online (Sandbox Code Playgroud)

小智 78

更新答案:

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

要解雇ProgressHUD:

MBProgressHUD.hideAllHUDs(for: view, animated: true)
Run Code Online (Sandbox Code Playgroud)

  • hideAllHUDs已弃用.我们应该在每个视图使用多个HUD时存储引用. (3认同)
  • 快速尝试这个适用于 iOS 的 HUD 库 https://github.com/shubh10/JustHUD (2认同)

The*_*imp 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...
    }
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*irk 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
Run Code Online (Sandbox Code Playgroud)


Har*_*ngh 6

我在UIViewController上添加了一个扩展,以使其更容易.

Swift 4.1

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!!!