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 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 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...
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.
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.
Activity Indicator not appears center on custom UIAlertView when search view controller push ups VC
Finally solved it by setting constraints by following way
func startActivityIndicator() {
self.view.isUserInteractionEnabled = false
let loadingView: UIView = UIView()
loadingView.translatesAutoresizingMaskIntoConstraints = false
actInd.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(loadingView)
loadingView.addSubview(actInd)
loadingView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
loadingView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
loadingView.widthAnchor.constraint(equalToConstant: 80.0).isActive = true
loadingView.heightAnchor.constraint(equalToConstant: 80.0).isActive = true
loadingView.center = self.view.center
loadingView.backgroundColor = UIColor(red: 44/255, green: 44/255, blue: 44/255, alpha: 0.7)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 10
actInd.leadingAnchor.constraint(equalTo: loadingView.leadingAnchor).isActive = true
actInd.trailingAnchor.constraint(equalTo: loadingView.trailingAnchor).isActive = true
actInd.topAnchor.constraint(equalTo: loadingView.topAnchor).isActive = true
actInd.bottomAnchor.constraint(equalTo: loadingView.bottomAnchor).isActive = true
actInd.style = .whiteLarge
actInd.center = CGPoint(x: loadingView.frame.size.width / 2, y: loadingView.frame.size.height / 2)
actInd.startAnimating()
}
How to add UIActivityIndicatorView at center of UIAlertView?
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];
Related Topics
Codable and Xmlparser in Swift
Building User Database Model in Firebase
Reading from the Clipboard with Swift 3 on MACos
Swift + Nsviewcontroller Background Color (MAC App)
Select Next Nstextfield with Tab Key in Swift
Making Cocoa Application Scriptable Swift
Using Just with Flatmap Produce Failure Mismatch. Combine
iOS 13.1 Uitextview Delegate Method Shouldinteract Called When Scrolling on Attachment
Executefetchrequest Doesn't Return the Nsmanagedobject Subclass
Swift Remove Object from Realm
How to Override Layerclass in Swift
Xcode 11 Doesn't Recognize Core Data Entity
Difference Between Dispatchqueue Types in Swift
How to Use Swift Package Manager in Xcode 9 Playground
Change Default Global Tint Color in Swift