Forte Zhu
Forte Zhu

Reputation: 762

Adding UIImage to UIAlertView is not working as expected

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?

Upvotes: 2

Views: 672

Answers (3)

Dilip Tiwari
Dilip Tiwari

Reputation: 1461

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

Upvotes: 0

Mukesh Lokare
Mukesh Lokare

Reputation: 2189

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.

Upvotes: 1

Jigar
Jigar

Reputation: 1821

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

Upvotes: 0

Related Questions