Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to show an Image in UIAlertView?

Is it possible to add image in an UIAlertView, like showing an image from the plist file?

like image 888
summer Avatar asked Feb 24 '10 03:02

summer


3 Answers

You can do it like:

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

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

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

    [successAlert addSubview:imageView];

    [successAlert show];

This will add a image in the right corner of your alertview you can change the frame image to move around.

Hope this helps.

like image 162
Madhup Singh Yadav Avatar answered Oct 14 '22 03:10

Madhup Singh Yadav


in iOS 7 or higher, use this code

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 282)];
UIImage *wonImage = [UIImage imageNamed:@"iberrys.png"];
imageView.contentMode=UIViewContentModeCenter;
[imageView setImage:wonImage];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Arirang List"
                                                     message:@"phiên bản: 1.0\n website: www.iberrys.com\n email: [email protected]\nmobile: 0918 956 456"
                                                    delegate:self
                                           cancelButtonTitle:@"Đồng ý"
                                           otherButtonTitles: nil];
//check if os version is 7 or above
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    [alertView setValue:imageView forKey:@"accessoryView"];
}else{
    [alertView addSubview:imageView];
}
 [alertView show];
like image 22
Quang Minh Avatar answered Oct 14 '22 04:10

Quang Minh


You'll need to subclass UIAlertView and rearrange its subviews a bit. There are several tutorials for this kind of stuff:

  • Custom UIAlertView (probably the most applicable to your problem)
  • Custom UIAlertView w/ TableView (also very handy)
like image 5
Rob Napier Avatar answered Oct 14 '22 04:10

Rob Napier