I've created a UIAlertView that contains a UIActivityIndicator. Everything works great, but I'd also like the UIAlertView to disappear after 5 seconds.
How can I Dismiss my UIAlertView after 5 seconds?
var alert: UIAlertView = UIAlertView(title: "Loading", message: "Please wait...", delegate: nil, cancelButtonTitle: "Cancel");
var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37)) as UIActivityIndicatorView
loadingIndicator.center = self.view.center;
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating();
alert.setValue(loadingIndicator, forKey: "accessoryView")
loadingIndicator.startAnimating()
alert.show()
I'm not an expert but this works for me and I guess is easier
A solution to dismiss an alert automatically in Swift 3 and Swift 4 (Inspired by part of these answers: [1], [2], [3]):
Result:
in swift 2 you can do this. Credit to @Lyndsey Scott
You can dismiss your
UIAlertView
after a 5 second delay programmatically, like so:Create the alert object as a global variable. You can use a
NSTimer
for this purpose.NOTE:
Reference : UIAlertView
For swift 4 you can use this code