Add an UIProgressView or UIActivityIndicatorView to your UIAlertView
November 9, 2008 – 22:26 Share on TwitterThis snippet shows how to add an UIProgressView to an UIAlertView.
You can choose to display an UIProgressView or an UIActivityIndicatorView in the alert.
This may break with a new version of the SDK (2.1 for now), but this was accepted by Apple (or they didnt see it)
- (void) createProgressionAlertWithMessage:(NSString *)message withActivity:(BOOL)activity
{
UIAlertView *progressAlert = [[UIAlertView alloc] initWithTitle: message
message: @"Please wait..."
delegate: self
cancelButtonTitle: nil
otherButtonTitles: nil];
// Create the progress bar and add it to the alert
if (activity) {
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityView.frame = CGRectMake(139.0f-18.0f, 80.0f, 37.0f, 37.0f);
[progressAlert addSubview:activityView];
[activityView startAnimating];
[activityView release];
} else {
UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30.0f, 80.0f, 225.0f, 90.0f)];
[progressAlert addSubview:progressView];
[progressView setProgressViewStyle: UIProgressViewStyleBar];
[progressView release];
}
[progressAlert show];
[progressAlert release];
}
Edit: corrected the code
7 Responses to “Add an UIProgressView or UIActivityIndicatorView to your UIAlertView”
I will check this out…
Usually progress view or indicators are used to show the something is happening in the background. So, i’m guessing that we’ll be calling this method in a separate thread.
How can i check if the progress alert is present in our view, and how can we command it to close?
By Mustafa on Jan 15, 2009
Just declare progressAlert as a variable of your class and check if it’s defined to see if the alert is present.
When you close it, set progressAlert to nil.
By Stephan on Jan 15, 2009
It worked for me, but i’m facing a little problem. Sometimes it jerks. Don’t really know the reason.
By the way, someone in the other community said that this approach is not recommended since Apple can reject the application since it’s not recommended. Recommended way is to create a new view and show the progress view or bar in that view and set the background to transparent.
Thanks for sharing your knowledge.
By Mustafa on Jan 17, 2009
Good point, thanks.
The only thing is, I would rather use à WhiteLarge as a style, and specify the center property instead of the frame, to avoid blurry resizing.
By simon maspero on May 14, 2009
Thanks!
By bcl on May 16, 2009
I think you’d also need to release the activityView/progressView after adding them as subviews of the progressAlert.
By Konrad on Nov 11, 2009
You’re absolutely right! And I need to declare the variables also. I’ve corrected the code.
By Stephan on Nov 11, 2009