How to Show Alertview with Activity Indicator

Adding Activity Indicator to UIAlertView

try this code!!

var alert: UIAlertView = UIAlertView(title: "Title", message: "Please wait...", delegate: nil, cancelButtonTitle: "Cancel")


var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 50, y: 10, width: 37, height: 37)) as UIActivityIndicatorView
loadingIndicator.center = self.view.center
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating();

alert.setValue(loadingIndicator, forKey: "accessoryView")
loadingIndicator.startAnimating()

alert.show();

I hope I helped you

How can I show alertview with activity indicator?

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...

How to display activity indicator in center of UIAlertController?

Be sure to set the frame property when you're creating a view.

func displaySignUpPendingAlert() -> UIAlertController {
//create an alert controller
let pending = UIAlertController(title: "Creating New User", message: nil, preferredStyle: .Alert)

//create an activity indicator
let indicator = UIActivityIndicatorView(frame: pending.view.bounds)
indicator.autoresizingMask = [.flexibleWidth, .flexibleHeight]

//add the activity indicator as a subview of the alert controller's view
pending.view.addSubview(indicator)
indicator.isUserInteractionEnabled = false // required otherwise if there buttons in the UIAlertController you will not be able to press them
indicator.startAnimating()

self.presentViewController(pending, animated: true, completion: nil)

return pending
}

To @62Shark:

let pending = UIAlertController(title: "Creating New User", message: nil, preferredStyle: .Alert)

let indicator = UIActivityIndicatorView()
indicator.setTranslatesAutoresizingMaskIntoConstraints(false)
pending.view.addSubview(indicator)

let views = ["pending" : pending.view, "indicator" : indicator]
var constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:[indicator]-(-50)-|", options: nil, metrics: nil, views: views)
constraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|[indicator]|", options: nil, metrics: nil, views: views)
pending.view.addConstraints(constraints)

indicator.userInteractionEnabled = false
indicator.startAnimating()

self.presentViewController(pending, animated: true, completion: nil)

how to place activity indicator in alert view when clicking button of that alert view

You can modify the UI control events for the OK button, so that your own event handler is called for that button and the alert view won't be dismissed until the long-running task has finished.
In that event handler attach an activity indicator to the view and start your task asynchronously using GCD.

#import <dispatch/dispatch.h>

// ...

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Test" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK" ,nil];

for (UIView *subview in alert.subviews)
{
if ([subview isKindOfClass:[UIControl class]] && subview.tag == 2) {
UIControl* button = (UIControl*) subview;
[button addTarget:self action:@selector(buttonOKPressed:) forControlEvents:UIControlEventTouchUpInside];
[button removeTarget:alert action:nil forControlEvents:UIControlEventAllEvents];
}
}
[alert show];
[alert release];

// ...

-(void) buttonOKPressed:(UIControl*) sender {

[sender removeTarget:self action:nil forControlEvents:UIControlEventAllEvents];
UIAlertView* alert = (UIAlertView*)[sender superview];
CGRect alertFrame = alert.frame;
UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicator.frame = CGRectMake(0,alertFrame.size.height, alertFrame.size.width,30);
activityIndicator.hidden = NO;
activityIndicator.alpha = 0.0;
activityIndicator.contentMode = UIViewContentModeCenter;
[activityIndicator startAnimating];
[alert addSubview:activityIndicator];
[activityIndicator release];
[UIView animateWithDuration:0.3 animations:^{
alert.frame = CGRectMake(alertFrame.origin.x, alertFrame.origin.y, alertFrame.size.width, alertFrame.size.height+50);
activityIndicator.alpha = 1.0;

}];
//alert.userInteractionEnabled = NO; // uncomment this, if you want to disable all buttons (cancel button)
dispatch_async(dispatch_get_global_queue(0,0), ^{
[NSThread sleepForTimeInterval:5]; // replace this with your long-running task
dispatch_async(dispatch_get_main_queue(), ^{
if (alert && alert.visible) {
[alert dismissWithClickedButtonIndex:alert.firstOtherButtonIndex animated:YES];
}
});
});
}

Alert View Activity Indicator Display Time

Here is an example of how I would go about solving the problem using NSTimer and NSInvocation. I have not tested it, so might have some trouble with it.

UIAlertView *Alert= [[UIAlertView alloc] initWithTitle:@"Loading"
message:@"\n"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(139.5, 75.5);
[Alert addSubview:spinner];
[spinner startAnimating];
[Alert show];
NSMethodSignature *mySignature = [UIAlertView instanceMethodSignatureForSelector:@selector(dismissWithClickedButtonIndex:animated:)];
NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:mySignature];
[myInvocation setSelector:@selector(dismissWithClickedButtonIndex:animated:)];
[myInvocation setTarget:Alert];
int buttonIndex = 0;
bool isAnimated = YES;
[myInvocation setArgument:&buttonClicked
atIndex:2];
[myInvocation setArgument:&isAnimated
atIndex:3];
NSTimer *t = [NSTimer scheduledTimerWithTimeInterval:2
invocation:myInvocation
repeats:NO];

I still do not think this is the best answer to the question though. In most cases you should use the indicator until the work to be done is complete, not based on a time, especially not a time that can change or dependent on other factors.

So the correct answer would be wait until the work is done and at that point dismiss the UIAlertView.

activity indicator on uialertview

Alert view reference: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html

In the delegate method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex you can add things to the alertView there.

E.g.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
UIActivityIndicator * ai = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[alertView addSubview:activityIndicator];
activityIndicator.frame = CGRectMake(80, 0, 30, 30);
[ai startAnimating];
}

You want to add ai as a ivar (class variable) in order to remove it later.

[self.ai removeFromSuperview];

Hope this helps.

Edit: one thing to keep in mind is that this way you will probably have to manage dismissing of your alert view yourself. You can use the method - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated on the alert view to dismiss it.

Reposition the Activity Indicator inside UIAlertView

There are two problems here. One is that you are making an assumption about the ultimate size of the progressAlert - you are positioning the activity indicator based on what the progressAlert frame is now, rather than based on what it will be when it appears. You can probably solve that by using constraints instead of an absolute center value.

However, the bigger problem is that what you are doing is illegal. Do not add your own subviews to a UIAlertView. Instead, make your own view controller with your own view and present it. It can look like an alert (i.e. be small, centered, dim out the rest of the interface) but now it is your view and you can do anything you like in it.



Related Topics



Leave a reply



Submit