Skip to content

Instantly share code, notes, and snippets.

@agiguere
Last active January 5, 2021 15:59
Show Gist options
  • Save agiguere/ae9bdbfafc111f998e992f1b26422cd0 to your computer and use it in GitHub Desktop.
Save agiguere/ae9bdbfafc111f998e992f1b26422cd0 to your computer and use it in GitHub Desktop.
MBProgressHUD Swift extension with dimmed background
import UIKit
public extension MBProgressHUD {
@objc func dimBackgroundView() {
backgroundView.style = .solidColor
backgroundView.color = UIColor(white: 0.0, alpha: 0.4)
bezelView.style = .solidColor
bezelView.backgroundColor = UIColor(white: 0.0, alpha: 0.9)
contentColor = .white
}
}
public protocol HUDLoadingIndicator: class {
var hudLoadingIndicator: MBProgressHUD? { get set }
}
public extension HUDLoadingIndicator where Self: UIViewController {
func showHUDLoadingIndicator(_ message: String = "", details: String? = nil) {
OperationQueue.main.addOperation({ [weak self] in
guard let `self` = self else { return }
guard self.navigationController != nil
else { self.hudLoadingIndicator = nil; return }
let hud = MBProgressHUD.showAdded(to: self.navigationController!.view, animated: true)
hud.label.text = message
hud.detailsLabel.text = details
hud.dimBackgroundView()
hud.show(animated: true)
self.hudLoadingIndicator = hud
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
})
}
func hideHUDLoadingIndicator() {
OperationQueue.main.addOperation({ [weak self] in
guard let `self` = self, let loadingIndicator = self.hudLoadingIndicator else { return }
loadingIndicator.hide(animated: true)
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment