To use MBProgressHUD in swift we first need to create a podfile if it does not exist already.
Go to terminal and change directory to your project directory then initialize pod and later install MBProgressHUD.
cd /projectDirectory pod init open podfile
Then in the podfile add the following line and go back to the terminal and run the below command in the same directory.
pod 'MBProgressHUD', '~> 1.1.0' pod install
Once you run these commands MBProgressHUD will be installed to your project, now you can import this library in ViewController where ever you want to use, or you may create an extension of UIView controller and use this method.
Let’s see this with two different methods, both of which will produce the same result.
1. Adding in ViewDidLoad
let Indicator = MBProgressHUD.showAdded(to: self.view, animated: true) Indicator.label.text = "Indicator" Indicator.isUserInteractionEnabled = false Indicator.detailsLabel.text = "fetching details" Indicator.show(animated: true)
Similarly, you may use the following to hide indicator from the view.
MBProgressHUD.hide(for: self.view, animated: true)
Let’s have a look at the second way of doing the same.
2. Creating an extension to make it globally accessible.
extension UIViewController { func showIndicator(withTitle title: String, and Description:String) { let Indicator = MBProgressHUD.showAdded(to: self.view, animated: true) Indicator.label.text = title Indicator.isUserInteractionEnabled = false Indicator.detailsLabel.text = Description Indicator.show(animated: true) } func hideIndicator() { MBProgressHUD.hide(for: self.view, animated: true) } }
When we run any of these on our device, we get the following result.