Reputation: 533
I'm using a UIAlertView with an Activity indicator inside of it, but the indicator is not positioned correctly. I'm sure that i'm missing something very small, but it would be great if any one can help me. Below are my code and corresponding code snippet
var progressAlert: UIAlertView = UIAlertView(title: "Downloading", message: "Patients information...", delegate: nil, cancelButtonTitle: nil);
var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
loadingIndicator.center = CGPointMake(progressAlert.bounds.size.width/2, progressAlert.bounds.size.height - 150)
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating()
progressAlert.setValue(loadingIndicator, forKey: "accessoryView")
progressAlert.show()
Edit: Following Matt's answer, ended up creating custom view and it works like a charm.
Upvotes: 3
Views: 1536
Reputation: 128
import Foundation
import UIKit
class YourFunction{
//#MARK: - showAlertView With Activity indicator
class func showAlertViewWithIndicator(TitleMessage: String, timeInterval: Double,view: UIViewController){
let alert = UIAlertView()
alert.delegate = self
alert.title = TitleMessage
let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37)) as UIActivityIndicatorView
loadingIndicator.center = view.view.center
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating();
alert.setValue(loadingIndicator, forKey: "accessoryView")
loadingIndicator.startAnimating()
alert.show()
let delay = timeInterval * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
alert.dismissWithClickedButtonIndex(-1, animated: true)
loadingIndicator.stopAnimating()
loadingIndicator.hidesWhenStopped = true
view.dismissViewControllerAnimated(true, completion: nil)
})
}
}
I wrote global functions for you. This will show UIAlertView with title and activity indicator. You can set the time based on TimeInterval variable. You can call it anywhere in your viewcontroller. Just type YourFunction.showAlertViewWithIndicator("Hello World",timeInterval:3.0,self)
It is for swift 2x Just wanna help :)
Upvotes: 0
Reputation: 534958
There are two problems here. One is that you are making an assumption about the ultimate size of the progressAlert
- you are positioning the activity indicator based on what the progressAlert
frame is now, rather than based on what it will be when it appears. You can probably solve that by using constraints instead of an absolute center
value.
However, the bigger problem is that what you are doing is illegal. Do not add your own subviews to a UIAlertView. Instead, make your own view controller with your own view and present it. It can look like an alert (i.e. be small, centered, dim out the rest of the interface) but now it is your view and you can do anything you like in it.
Upvotes: 1