如何在UIAlertController中添加UIImageView?

时间:2016-06-16 01:02:12

标签: ios objective-c uiimageview uialertcontroller

我希望UIAlertControllerUIImageView中显示ActionSheet的提醒。但是当我运行应用程序时,它正在终止。

这是我的代码:

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

2 个答案:

答案 0 :(得分:4)

根据Apple Doc。

,无法将图像添加到UIAlertController

如果您愿意,可以创建自己的自定义视图,如:

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

如果您想在button上显示图片 然后尝试这样:

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

答案 1 :(得分:0)

更改此内容:

UIImageView *imgv=[[UIImageView alloc]initWithFrame:CGRectMake(20,20,50,50)];
    imgv.image=[UIImage imageNamed:@"kaga.jpg"];
    [alert setValue:imgv  forKey:@"image"];

对此:

UIImage *img= [UIImage imageNamed:@"kaga.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [alert setValue:img  forKey:@"image"];