2

I am working on an app. I tried to add an UIIMage to UIAlertView using below code,

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Instruction"
                                                    message:@"Please TAP on Screen to Continue."
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 50, 32, 32)];
UIImage *img = [UIImage imageNamed:@"pendingImg.png"];
[imageView setImage:img];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    [alert setValue:imageView forKey:@"accessoryView"];
}else{
    [alert addSubview:imageView];
}
[alert show];

My image is of dimension 32 × 32 pixels. I am getting the alert as shown,

Alert

Should I add constraints to this image? or anything else to be made?

Forte Zhu
  • 742
  • 3
  • 12
  • 33

3 Answers3

1

You can set imageView contentMode to UIViewContentModeScaleAspectFit,

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Instruction"
                                                    message:@"Please TAP on Screen to Continue."
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 50, 32, 32)];
UIImage *img = [UIImage imageNamed:@"pendingImg.png"];
 imageView.contentMode = UIViewContentModeScaleAspectFit;

[imageView setImage:img];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    [alert setValue:imageView forKey:@"accessoryView"];
}else{
    [alert addSubview:imageView];
}
[alert show];

enter image description here

Might be Anbu.Karthik suggested link comment also helpful for you.

Mukesh Lokare
  • 2,159
  • 26
  • 38
0

try this

 UIAlertView* alert = [UIAlertView alloc] initWithTitle: @"Instruction" message: @"Please TAP on Screen to Continue." delegate:nil cancelButtonTitle:@"no" otherButtonTitles:@"yes", nil];

    UIImage* img = [UIImage imageNamed:@"pendingImg.png"];
    UIImageView* imgview = [UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
    [img setImage:imgMyImage];

    [alert setValue: imgview forKey:@"accessoryView"];
    [alert show];
Jigar
  • 1,801
  • 1
  • 14
  • 29
0
UIAlertView* alert = [[UIAlertView alloc] initWithTitle: @"Instruction" message: @"Please TAP on Screen to Continue." delegate:nil cancelButtonTitle:@"no" otherButtonTitles:@"yes", nil];

   UIImage* img = [UIImage imageNamed:@"add.png"];
    UIImageView* imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
    [imgview setImage:img];
    [alert setValue: imgview forKey:@"accessoryView"];
    [alert show];
}
Dilip Tiwari
  • 1,441
  • 18
  • 31