向UIAlertView添加活动指示器

时间:2014-12-18 21:35:17

标签: swift uialertview uiactivityindicatorview

我正在尝试将UIActivityIndicatorView添加到UIAlertView,但无法完成。我看过这个实现的帖子,发现它只适用于iOS7以下的版本。

以下是我尝试的代码......

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

var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0.0, 0.0, 10.0, 10.0)) as UIActivityIndicatorView
loadingIndicator.center = self.view.center;
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating();

alert.addSubview(loadingIndicator);  
alert.show();

为了完成这项工作,iOS7及以上版本有什么具体的事情要做吗?

4 个答案:

答案 0 :(得分:27)

试试这段代码!!

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


        var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 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();

我希望我能帮到你

答案 1 :(得分:5)

对于swift 4

全局

extension UIAlertController {

    private struct ActivityIndicatorData {
        static var activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
    }

    func addActivityIndicator() {
        let vc = UIViewController()
        vc.preferredContentSize = CGSize(width: 40,height: 40)
        ActivityIndicatorData.activityIndicator.color = UIColor.blue
        ActivityIndicatorData.activityIndicator.startAnimating()
        vc.view.addSubview(ActivityIndicatorData.activityIndicator)
        self.setValue(vc, forKey: "contentViewController")
    }

    func dismissActivityIndicator() {
        ActivityIndicatorData.activityIndicator.stopAnimating()
        self.dismiss(animated: false)
    }
}

var activityIndicatorAlert: UIAlertController?

func displayActivityIndicatorAlert() {
    activityIndicatorAlert = UIAlertController(title: NSLocalizedString("Loading", comment: ""), message: NSLocalizedString("PleaseWait", comment: "") + "...", preferredStyle: UIAlertControllerStyle.alert)
    activityIndicatorAlert!.addActivityIndicator()
    var topController:UIViewController = UIApplication.shared.keyWindow!.rootViewController!
    while ((topController.presentedViewController) != nil) {
        topController = topController.presentedViewController!
    }
    topController.present(activityIndicatorAlert!, animated:true, completion:nil)
}

func dismissActivityIndicatorAlert() {
    activityIndicatorAlert!.dismissActivityIndicator()
    activityIndicatorAlert = nil
}

答案 2 :(得分:1)

对于那些使用Objective-C的人。我的解决方案将UIActivityIndicatorView包含更大的视图,因此UIAlertView边缘的边距不会那么小。

#import "UIAlertView+ActivityIndicator.h"

@implementation UIAlertView (ActivityIndicator)

+ (UIAlertView *)alertViewWithActivityIndicator:(NSString *)title delegate:(id<UIAlertViewDelegate>)delegate;
{
    UIAlertView *a = [[UIAlertView alloc] initWithTitle:title
                                                message:nil
                                               delegate:delegate
                                      cancelButtonTitle:nil
                                      otherButtonTitles:nil];

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    aiv.translatesAutoresizingMaskIntoConstraints = NO;
    [v addSubview:aiv];
    [v addConstraint:[NSLayoutConstraint constraintWithItem:aiv
                                                  attribute:NSLayoutAttributeCenterX
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:v
                                                  attribute:NSLayoutAttributeCenterX
                                                 multiplier:1.0
                                                   constant:0]];
    [v addConstraint:[NSLayoutConstraint constraintWithItem:aiv
                                                  attribute:NSLayoutAttributeCenterY
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:v
                                                  attribute:NSLayoutAttributeCenterY
                                                 multiplier:1.0
                                                   constant:0]];
    [aiv startAnimating];
    [a setValue:v forKey:@"accessoryView"];

    return a;
}

@end

答案 3 :(得分:1)

Swift 3中接受的答案:

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


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

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

    alert.show();