Last active
January 5, 2021 15:59
-
-
Save agiguere/ae9bdbfafc111f998e992f1b26422cd0 to your computer and use it in GitHub Desktop.
MBProgressHUD Swift extension with dimmed background
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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