How to use MBProgressHUD with swift

后端 未结 10 1622
北海茫月
北海茫月 2020-12-08 02:23

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          


        
相关标签:
10条回答
  • 2020-12-08 02:33

    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...
        }
    }
    
    0 讨论(0)
  • 2020-12-08 02:36

    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)
    
    0 讨论(0)
  • 2020-12-08 02:40

    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)

    0 讨论(0)
  • 2020-12-08 02:43

    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
    
    0 讨论(0)
  • 2020-12-08 02:49

    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)
    }
    
    0 讨论(0)
  • 2020-12-08 02:50

    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)
    
    0 讨论(0)
提交回复
热议问题