22

I want to show alertview with message: "Loading data" and spinning activity indicator. How can I do this?

János
  • 32,867
  • 38
  • 193
  • 353
Voloda2
  • 12,359
  • 18
  • 80
  • 130

7 Answers7

27

NOTE: This solution won't work on iOS 7 and above.

This is my take on it:

alertView = [[UIAlertView alloc] initWithTitle:@"Submitting Entry"
                                       message:@"\n"
                                      delegate:self
                             cancelButtonTitle:nil
                             otherButtonTitles:nil];

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];   
spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[alertView addSubview:spinner];
[spinner startAnimating];
[alertView show];

and dismiss in code using:

[alertView dismissWithClickedButtonIndex:0 animated:YES];
jowie
  • 8,028
  • 8
  • 55
  • 94
  • 5
    use ` spinner.center = CGPointMake(0.0, -spinner.frame.size.height); spinner.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;` to always position the spinner at the bottom center of your alert – MrTJ Feb 07 '13 at 17:21
  • spinner doesn't show. My alterView only shows Submitting Entry. I am using iOS 7. – coolcool1994 Jul 02 '14 at 07:14
  • @coolcool1994 This has not been tested on iOS 7. Answer from 2 years ago ;) – jowie Jul 02 '14 at 11:27
  • That's true :) I hope I could know the iOS7 way of this :} – coolcool1994 Jul 02 '14 at 17:08
25

This works on iOS 7

addSubView doesn't work on UIAlertView in iOS 7 and above. Try this instead

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];
AndyDeveloper
  • 2,790
  • 1
  • 21
  • 23
20

you can add a label and activityindicator as subviews of your alert view. you have to do some thing like this

myAlertView = [[UIAlertView alloc] initWithTitle:@"Loading" message:@"\n\n"
                                        delegate:self
                               cancelButtonTitle:@""
                               otherButtonTitles:@"OK", nil];  

UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc]
                initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];   
loading.frame=CGRectMake(150, 150, 16, 16);
[myAlertView addSubview:loading];
[myAlertView show];

..better to use a UIActionSheet in this situation...

khunshan
  • 2,662
  • 4
  • 28
  • 34
Sat
  • 1,616
  • 3
  • 22
  • 40
  • thank you , i will use UIActionSheet + UIActivityIndicatorView – Voloda2 Feb 05 '11 at 13:04
  • 1
    Why should you use a UIActionSheet in this situation? (I know Apple would probably agree with you, but I'd like to know why). You're not using any private APIs, right? – Nate Jul 24 '11 at 03:53
  • 3
    You're missing `[loading startAnimating]` and also there are empty buttons showing. – jowie May 24 '12 at 10:22
  • you would have missed [loading startAnimating]; i guess – dinesh Aug 23 '13 at 09:52
6

you can add a label and activityindicator as subviews of your alert view. you have to do some thing like this...

- (IBAction)showAlertWithActivity:(id)sender{

alerta = [[UIAlertView alloc] initWithTitle:@"Guardando datos..."
                                            message:@"\n"
                                           delegate:self
                                  cancelButtonTitle:nil
                                  otherButtonTitles:nil];

        UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
        [alerta addSubview:spinner];
        [spinner startAnimating];
        [alerta show];


        [self performSelector:@selector(close) withObject:self afterDelay:1];


    }
    -(void)close{

        [alerta dismissWithClickedButtonIndex:0 animated:YES];

    }
oscar castellon
  • 3,048
  • 30
  • 19
1

Add This in your .h file UIAlertView *connectingAlert;

And Add these two functions in your .m files

//show loading activity.
- (void)startSpinner:(NSString *)message {
    //  Purchasing Spinner.
    if (!connectingAlert) {
        connectingAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(message,@"")
                                                 message:nil
                                                delegate:self
                                       cancelButtonTitle:nil
                                       otherButtonTitles:nil];
        connectingAlert.tag = (NSUInteger)300;
        [connectingAlert show];

        UIActivityIndicatorView *connectingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        connectingIndicator.frame = CGRectMake(139.0f-18.0f,50.0f,37.0f,37.0f);
        [connectingAlert addSubview:connectingIndicator];
        [connectingIndicator startAnimating];

    }
}
//hide loading activity.
- (void)stopSpinner {
    if (connectingAlert) {
        [connectingAlert dismissWithClickedButtonIndex:0 animated:YES];
        connectingAlert = nil;
    }
    // [self performSelector:@selector(showBadNews:) withObject:error afterDelay:0.1];
}

then call

[self startSpinner:@"Your message........"];
[self stopSpinner];
Raees Madathil
  • 291
  • 1
  • 3
  • 14
1

In Swift 3

let loadingAlertController = UIAlertController(title: "Loading", message: nil, preferredStyle: .alert)

let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
activityIndicator.translatesAutoresizingMaskIntoConstraints = false

loadingAlertController.view.addSubview(activityIndicator)

let xConstraint = NSLayoutConstraint(item: activityIndicator, attribute: .centerX, relatedBy: .equal, toItem: loadingAlertController.view, attribute: .centerX, multiplier: 1, constant: 0)

let yConstraint = NSLayoutConstraint(item: activityIndicator, attribute: .centerY, relatedBy: .equal, toItem: loadingAlertController.view, attribute: .centerY, multiplier: 1.4, constant: 0)

NSLayoutConstraint.activate([ xConstraint, yConstraint])

activityIndicator.isUserInteractionEnabled = false
activityIndicator.startAnimating()

let height: NSLayoutConstraint = NSLayoutConstraint(item: loadingAlertController.view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 80)

loadingAlertController.view.addConstraint(height);

self.present(loadingAlertController, animated: true, completion: nil)
pableiros
  • 14,932
  • 12
  • 99
  • 105
0
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alarm" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];

UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc]
                                    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];   
loading.frame=CGRectMake(125, 50, 36, 36);
[loading startAnimating];
[alert addSubview:loading];
[alert show];
Tisho
  • 8,320
  • 6
  • 44
  • 52
Salman Iftikhar
  • 311
  • 2
  • 9