How to display activity indicator in center of UIAlertController?

后端 未结 13 985
猫巷女王i
猫巷女王i 2020-12-08 05:18

I currently have a UIAlertController being displayed on the screen. The view of the alert should only display 2 elements, a title and a UIActivityIndicato

相关标签:
13条回答
  • 2020-12-08 05:32

    Well try this code.

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
                                        message:@"Creating new user\n\n\n"
                                 preferredStyle:UIAlertControllerStyleAlert];
    
    UIActivityIndicatorView *loader = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    loader.center = CGPointMake(130.5, 65.5);
    loader.color = [UIColor blackColor];
    [loader startAnimating];
    [alert.view loader];
    [self presentViewController:alert animated:NO completion:nil];
    
    0 讨论(0)
  • 2020-12-08 05:34

    Try this:

    activityView.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height / 2.0)
    

    Also you will need to check for landscape mode and reverse width and height.

    if(landscapeMode)activityView.center = CGPointMake(self.view.bounds.size.height/2.0, self.view.bounds.size.width / 2.0)
    

    Maybe you can get the alert view position?

    alert.view.frame.origin.x
    alert.view.frame.origin.y
    

    and use that to place your activity view dynamically ie with the variables?

    Of course you might also want to get the size divide by 2 and add that so that its centred as well.

    alert.view.frame.size.height
    alert.view.frame.size.width
    
    0 讨论(0)
  • 2020-12-08 05:38

    In swift:

    activityIndicator.center = self.view.center
    

    If you have a tool bar or a navController you might want to shift the point but otherwise, center is center...

    If you still have issues, perhaps this tutorial would help. If you are trying to center it in a table view controller, this answer might help.

    0 讨论(0)
  • 2020-12-08 05:41

    Apple does not encourage directly subclassing UIAlertController so I made a class that displays UIAlertController with centered UIActivityIndicator and handles the cancel condition with a class protocol.

    import Foundation
    import UIKit
    
    protocol BusyAlertDelegate {
        func didCancelBusyAlert()
    }
    
    
    class BusyAlert {
    
       var busyAlertController: UIAlertController?
       var presentingViewController: UIViewController?
       var activityIndicator: UIActivityIndicatorView?
       var delegate:BusyAlertDelegate?
    
       init (title:String, message:String, presentingViewController: UIViewController) {
           busyAlertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
           busyAlertController!.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "Cancel Button"), style: UIAlertActionStyle.Cancel, handler:{(alert: UIAlertAction!) in
                delegate?.didCancelBusyAlert()
        }))
            self.presentingViewController = presentingViewController
            activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
            busyAlertController!.view.addSubview(activityIndicator!)
        }
    
        func display() {
            dispatch_async(dispatch_get_main_queue(), {
                   self.presentingViewController!.presentViewController(self.busyAlertController!, animated: true, completion: {
                self.activityIndicator!.translatesAutoresizingMaskIntoConstraints = false
                   self.busyAlertController!.view.addConstraint(NSLayoutConstraint(item: self.activityIndicator!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.busyAlertController!.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
                self.busyAlertController!.view.addConstraint(NSLayoutConstraint(item: self.activityIndicator!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.busyAlertController!.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
                self.activityIndicator!.startAnimating()
    
            })
        })
    
    }
    
    func dismiss() {
        dispatch_async(dispatch_get_main_queue(), {
            self.busyAlertController?.dismissViewControllerAnimated(true, completion: nil)
        })
    }
    
    }
    

    I recommend using lazy var to initialize the class.

    lazy var busyAlertController: BusyAlert = {
            let busyAlert = BusyAlert(title: "Lengthy Task", message: "Please     wait...", presentingViewController: self)
            busyAlert.delegate = self
            return busyAlert
            }()
    

    Here is a link to sample code: https://github.com/cgilleeny/BusyAlertExample.git

    0 讨论(0)
  • 2020-12-08 05:41

    I had the same problem and using frame positioning didn't work for me. Yimin Lin's answer was very close for me, but I just wanted to present an alternative using constraints in non-visual format:

    //...
    indicator.setTranslatesAutoresizingMaskIntoConstraints(false)
    alert.view.addSubview(indicator)
    
    alert.view.addConstraint(NSLayoutConstraint(item: indicator, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: alert.view, attribute: attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    alert.view.addConstraint(NSLayoutConstraint(item: indicator, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: alert.view, attribute: attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
    //...
    
    0 讨论(0)
  • 2020-12-08 05:42

    For those like me who prefer UIActivityIndicatorView aligned at the left of the UIAlertController.title, this is my solution in Swift working for all devices:

    let alert = UIAlertController(title: NSLocalizedString("Authenticating...", comment: "Authenticating"), message: nil, preferredStyle: .Alert);
    let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
    activityIndicator.frame = activityIndicator.frame.rectByOffsetting(dx: 8, dy: (alert.view.bounds.height - activityIndicator.frame.height)/2);
    activityIndicator.autoresizingMask = .FlexibleRightMargin | .FlexibleTopMargin | .FlexibleBottomMargin
    activityIndicator.color = themeManager().currentTheme.navigationBarTintColor;
    activityIndicator.startAnimating();
    alert.view.addSubview(activityIndicator);
    self.presentViewController(progressAlert, animated: true, completion: nil);
    

    However, to align the UIActivityIndicatorView in the view center you can change as follows:

    activityIndicator.center = CGPoint(x: (alert.view.bounds.width)/2, y: (alert.view.bounds.height)/2)
    activityIndicator.autoresizingMask = .FlexibleLeftMargin | .FlexibleRightMargin | .FlexibleTopMargin | .FlexibleBottomMargin
    
    0 讨论(0)
提交回复
热议问题