Adding Activity Indicator to UIAlertView

Srujan Simha picture Srujan Simha · Dec 18, 2014 · Viewed 16.6k times · Source

I'm trying to add UIActivityIndicatorView to UIAlertView but couldn't get it done. I have seen posts on this implementation and found out that it works only for versions below iOS7.

Below is the code I've tried...

var alert: UIAlertView = UIAlertView(title: "Title", message: "Please wait...", delegate: nil, cancelButtonTitle: "Cancel");

var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0.0, 0.0, 10.0, 10.0)) as UIActivityIndicatorView
loadingIndicator.center = self.view.center;
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating();

alert.addSubview(loadingIndicator);  
alert.show();

Is there anything specific thing to do for iOS7 and above to get this done?

Answer

Beslan Tularov picture Beslan Tularov · Dec 18, 2014

try this code!!

var alert: UIAlertView = UIAlertView(title: "Title", message: "Please wait...", delegate: nil, cancelButtonTitle: "Cancel")


var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 50, y: 10, width: 37, height: 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 hope I helped you