1

I want to add image at the top left corner of the UIAlertView.

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];

I am using this code but image does not come up with UIAlertView. Please help me

Moin Shirazi
  • 4,276
  • 2
  • 25
  • 37
Prashant Sharma
  • 1,287
  • 1
  • 21
  • 30

3 Answers3

1

For iOS 7.0 +

Use:

[successAlert setValue:imageView forKey:@"accessoryView"];
Flexo
  • 84,884
  • 22
  • 182
  • 268
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];
Akash KR
  • 738
  • 3
  • 11
1

First add your image to image assets and give name as imgCloud Then update your code to :

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];
Pushkraj P. Lanjekar
  • 2,183
  • 1
  • 20
  • 33