2014-12-18 15 views
5

Ich versuche UIActivityIndicatorView zu UIAlertView hinzuzufügen, konnte es aber nicht schaffen. Ich habe Beiträge zu dieser Implementierung gesehen und festgestellt, dass sie nur für Versionen unter iOS7 funktioniert.Aktivitätsanzeige zu UIAlertView hinzufügen

Unten ist der Code Ich habe versucht ...

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(); 

Gibt es etwas Bestimmtes, was für iOS7 zu tun und darüber zu tun bekommen?

+2

Sie auch in dieser Klasse interessiert sein könnte: https://github.com/goktugyil/CozyLoadingActivity – Esqarrouth

Antwort

23

versuchen Sie diesen Code !!

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(); 

Ich hoffe, dass ich Ihnen

geholfen
+0

Perfect! Vielen Dank. –

+0

Immer glücklich zu helfen –

+0

eine sehr kurze, sehr gute Lösung ohne zu empfehlen, einen Pod zu verwenden. Du verdienst einen besonderen Platz im Himmel. –

0

Für diejenigen mit Objective-C. Meine Lösung wickelt die UIActivityIndicatorView mit einer größeren Ansicht, so dass der Rand bis zum Rand von UIAlertView nicht so klein ist.

#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 
1

Die akzeptierte Antwort in 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(); 
+1

UIAlertView ist in iOS 9.0 veraltet – cmii

Verwandte Themen