Add an UIProgressView or UIActivityIndicatorView to your UIAlertView

This 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

10 comments on “Add an UIProgressView or UIActivityIndicatorView to your UIAlertView

  1. Mustafa

    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?

  2. Stephan

    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.

  3. Mustafa

    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.

  4. simon maspero

    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.

  5. Konrad

    I think you’d also need to release the activityView/progressView after adding them as subviews of the progressAlert.

  6. Alexander

    Interesting post. It almost works for me, but I wanted to add a cancel button to stop to dismiss the dialog. When I do the progress view is overlapped by the button. In other words, it doesn’t look like one can control the size of the alert view.

  7. tyler

    Thanks it works, but just the activity indicator not he progress bar…

    this in xcode 4.5 and ios 5

Leave a Reply

Your email address will not be published. Required fields are marked *