2016-06-20 20 views
1

Ich möchte Bild in der oberen linken Ecke des UIAlertView hinzufügen.Wie Bild in UIAlertView hinzufügen?

UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)]; 

NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"cloud.png"]]; 
UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path]; 
[imageView setImage:bkgImg]; 


[successAlert addSubview:imageView]; 

[successAlert show]; 

Ich benutze diesen Code aber Bild kommt nicht mit UIAlertView. Bitte helfen Sie mir

Antwort

1
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"your Title" message:@"Your Message" delegate:nil cancelButtonTitle:@"Your Title" otherButtonTitles:nil]; 

UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 40, 40)]; 

NSString *loc = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Your Image Name"]]; 
UIImage *img = [[UIImage alloc] initWithContentsOfFile:loc]; 
[image setImage:img]; 

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { 
    [Alert setValue:image forKey:@"accessoryView"]; 
}else{ 
    [Alert addSubview:image]; 
} 

[Alert show]; 
+0

danke für Ihre Hilfe. Jetzt mit diesem Code.Image ist in der Alert-Ansicht, kam aber unten und wurde von links nach rechts gestreckt. –

+0

Ändern Sie den Bildbetrachtungsrahmen –

1

Für iOS 7,0 +

Verwendung:

[successAlert setValue:imageView forKey:@"accessoryView"]; 
1

Zuerst fügen Sie Ihre Bild-Assets zu Bild und geben Namen wie imgCloud dann Ihren Code aktualisieren, um:

UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

UIImageView *imageViewCloud = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)]; 
UIImage *bkgImg = [UIImage imageNamed:@"imgCloud"]; 
[imageViewCloud setImage:bkgImg]; 
[successAlert imageViewCloud forKey:@"accessoryView"]; 
[successAlert show];