15

I want to create a UIAlertView that will say that "...in progress". It will also show that UIActivityindicatorView on it. Could you let me know how can I do that?

Thanks.

ebaccount
  • 5,051
  • 11
  • 43
  • 47

3 Answers3

41

Its pretty simple. Just create a UIActivityIndicatorView and add it as a subview to the UIAlertView.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" " message:@" " delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
    progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    [alert addSubview:progress];
    [progress startAnimating];

    [alert show];
Brandon Schlenker
  • 5,078
  • 1
  • 36
  • 58
2

In my case i found what using hardcoded frame origins it's bad. And if my message has more one line, indicator showing top of my message.

So i create function with layouting indicator if size UIAlertView

+(UIAlertView*) progressAlertWithTitle:(NSString*) title andMessage:(NSString*) message andDelegate:(id)delegate{
    UIAlertView *progressAlert = [[UIAlertView alloc] init];
    [progressAlert setTitle:title];
    [progressAlert setMessage:message];
    [progressAlert setDelegate:delegate];
    UIActivityIndicatorView *progress=nil;
    progress= [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    [progressAlert addSubview:progress];
    [progress startAnimating];

    [progressAlert show];

    progress.frame=CGRectMake(progressAlert.frame.size.width/2-progress.frame.size.width, progressAlert.frame.size.height-progress.frame.size.height*2, progress.frame.size.width, progress.frame.size.height);


    return progressAlert;
}

In this case, indicator always by center

One line message:

enter image description here

More one line message:

enter image description here

Dmitry Nelepov
  • 7,246
  • 8
  • 53
  • 74
0
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView   *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGRectMake(xcords, ycords, width, height);
[alert addSubview:spinner];
[spinner startanimating];
[alert show];

This spinner gets hidden on dismiss of AlertView.

[alert dismissWithClickedButtonIndex:0 animated:YES];
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177