Skip to content

Instantly share code, notes, and snippets.

@amfathi
Last active March 20, 2021 12:22
Show Gist options
  • Save amfathi/c3477239095dac1bb3c63cf64efe476d to your computer and use it in GitHub Desktop.
Save amfathi/c3477239095dac1bb3c63cf64efe476d to your computer and use it in GitHub Desktop.
Convenient ProgressView in UIAlertController to block the UI till completion.
import UIKit
class ProgressView {
private lazy var progressView: UIProgressView = {
let progressView = UIProgressView(frame: CGRect(x: 15, y: 20, width: 240, height: 8))
return progressView
}()
private lazy var alert: UIAlertController = {
let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)
alert.view.addSubview(progressView)
return alert
}()
private weak var presentingViewController: UIViewController?
init(presentingViewController: UIViewController) {
self.presentingViewController = presentingViewController
}
func present() {
progressView.progress = 0
presentingViewController?.present(alert, animated: true)
}
func setProgress(_ progress: Float) {
progressView.progress = progress
}
func dismiss(completion: (() -> Void)? = nil) {
alert.dismiss(animated: true, completion: completion)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment