Yasuhiro Kondo Yasuhiro Kondo - 5 months ago 202
Objective-C Question

How to add UIImageView in UIAlertController?

I want to

UIAlertController
present alert with UIImageView in ActionSheet. but when I run the application then it's Terminating....

This is my code:

UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Title"
message:@"Welcome"
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* okButton = [UIAlertAction
actionWithTitle:OK
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[alert addAction:okButton];
UIImageView *imgv=[[UIImageView alloc]initWithFrame:CGRectMake(20,20,50,50)];
imgv.image=[UIImage imageNamed:@"kaga.jpg"];
[alert setValue:imgv forKey:@"image"];
[self presentViewController:alert animated:YES completion:nil];

Answer

It is not possible to add an image to a UIAlertController according to Apple Doc.

if you want then you can create your own custom view like:

https://github.com/wimagguc/ios-custom-alertview

If you want to take image appear on button then Try like this:

UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:@"Title"
                                  message:@"Welcome"
                                  preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction* okButton = [UIAlertAction
                                actionWithTitle:OK
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)
                                {
//Do some thing here
                         [alert dismissViewControllerAnimated:YES completion:nil];

                                }];

[okButton setValue:[[UIImage imageNamed:@"kaga.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];

[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];