1

As the title, I want to add an UIActivityIndicatorView instance at center of an UIAlertView instance. Here is my code:

    alertView  = [[UIAlertView alloc]initWithTitle:@"Processing" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
indicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(screenRect.size.width/2, screenRect.size.height/2,50,50)];
[alertView addSubview: indicator];
[indicator startAnimating];
[alertView show];

What I see is just the alertView. Did I make any mistake?

jww
  • 97,681
  • 90
  • 411
  • 885
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165

3 Answers3

11

You have forgotten to take into account the indicator's width and height, when setting it's x and y position in the alertview frame.

indicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(roundf((screenRect.size.width - 50) / 2), roundf((screenRect.size.height - 50) / 2),50,50)];

EDIT: This is the exact one I typically use: (don't forget to release things, etc)

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Loading..." message: nil 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];
Luke
  • 11,426
  • 43
  • 60
  • 69
  • Isn't it easier to set the `center` property for `indicator`? – Scott Berrevoets Mar 13 '13 at 16:49
  • I tried this, but it didn't help. When I set indicator.center = alertView.center;, the indicator always show at the top left corner of alertview – Nguyen Minh Binh Mar 13 '13 at 16:49
  • @Scott Perhaps, it's a matter of taste. :) @ Nguyen I have edited my answer... did you set the style of the indicator view? – Luke Mar 13 '13 at 16:52
  • 1
    Great. your editing helps. But I have to wait 4 more minutes to accept this answer. Thanks a bunch. :) – Nguyen Minh Binh Mar 13 '13 at 16:53
  • 1
    I'll just leave this here in case someone needs to dismiss this from another method programmatically: [alert dismissWithClickedButtonIndex:0 animated:YES]; – LordParsley Nov 09 '13 at 15:15
  • If you're on iOS 7, this won't work and you'll want to check this out: http://stackoverflow.com/questions/18895106/alert-view-is-showing-white-rectangle-in-ios7 – LordParsley Nov 09 '13 at 15:32
  • I don't use the above any more either; I changed to MBProgressHUD which works just fine on iOS 7. https://github.com/jdg/MBProgressHUD – Luke Nov 09 '13 at 18:13
3

WORKS IOS 7+

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Loading data" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[indicator startAnimating];

[alertView setValue:indicator forKey:@"accessoryView"];
[alertView show];

and to dismiss it

[alertView dismissWithClickedButtonIndex:0 animated:YES];
Bhanu
  • 1,249
  • 10
  • 17
Cayke Prudente
  • 220
  • 3
  • 5
0
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Loading..." message: nil 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];