ios-将图像添加到UIAlertCon中的UIAlertAction

我已经看到了UIAlertControllers的几个屏幕截图,在该行的左侧有一个图像,但是我在文档中没有看到它。 视觉示例是http://imgur.com/SISBvjB这是我现在为控制器使用的代码:

UIAlertController * view =   [UIAlertController
                                 alertControllerWithTitle:@"My Title"
                                 message:@"Select you Choice"
                                 preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction* ok = [UIAlertAction
                         actionWithTitle:@"OK"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                          }];
    [view addAction:ok];
    [self presentViewController:view animated:YES completion:nil];
user1079052 asked 2020-01-14T12:44:29Z
7个解决方案
93 votes

就是这样完成的:

let image = UIImage(named: "myImage")
var action = UIAlertAction(title: "title", style: .default, handler: nil)
action.setValue(image, forKey: "image")
alert.addAction(action)

image属性未公开,因此无法保证在将来的版本中都可以使用,但到目前为止仍可以正常工作

JP Hribovsek answered 2020-01-14T12:44:52Z
25 votes
UIAlertController * view=   [UIAlertController
                             alertControllerWithTitle:@"Staus ! "
                             message:@"Select your current status"
                             preferredStyle:UIAlertControllerStyleActionSheet];


UIAlertAction* online = [UIAlertAction
                     actionWithTitle:@"Online"
                     style:UIAlertActionStyleDefault
                     handler:^(UIAlertAction * action)
                     {
                         //Do some thing here
                         [view dismissViewControllerAnimated:YES completion:nil];

                     }];
UIAlertAction* offline = [UIAlertAction
                         actionWithTitle:@"Offline"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             [view dismissViewControllerAnimated:YES completion:nil];

                         }];
UIAlertAction* doNotDistrbe = [UIAlertAction
                         actionWithTitle:@"Do not disturb"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             [view dismissViewControllerAnimated:YES completion:nil];

                         }];
UIAlertAction* away = [UIAlertAction
                               actionWithTitle:@"Do not disturb"
                               style:UIAlertActionStyleDestructive
                               handler:^(UIAlertAction * action)
                               {
                                   [view dismissViewControllerAnimated:YES completion:nil];

                               }];

[online setValue:[[UIImage imageNamed:@"online.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[offline setValue:[[UIImage imageNamed:@"offline.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[doNotDistrbe setValue:[[UIImage imageNamed:@"DND.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[away setValue:[[UIImage imageNamed:@"away.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];





[view addAction:online];
[view addAction:away];
[view addAction:offline];
[view addAction:doNotDistrbe];
[self presentViewController:view animated:YES completion:nil];
dheerendra answered 2020-01-14T12:45:08Z
10 votes

尝试这样的事情:

UIAlertView* alert = [UIAlertView alloc] initWithTitle: @"Test Alert" message: @"Alert With Custom View" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];

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

[alert setValue: ivMyImageView forKey:@"accessoryView"];
[alert show];

经过测试,适用于iOS 7.0

enter image description here

PruitIgoe answered 2020-01-14T12:45:32Z
8 votes

您可以通过将UIAlertController子类化并在标题字符串中添加\n到标题字符串上来在标题标签上方添加图像,以为UIImageView留出空间。您必须根据字体大小来计算布局。 对于UIAlertAction中的图像,请像self.setValue(image, forKey: "image")一样使用KVC。我建议使用扩展名检查responds(to:)。这是示例实现。

enter image description here

stringCode answered 2020-01-14T12:45:53Z
7 votes

为了迅速

let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let action = UIAlertAction(title: NSLocalizedString("Share", comment: ""), style: .default, handler: { _ in
        })
let image = UIImage(named: "Temp1")
action.setValue(image?.withRenderingMode(.alwaysOriginal), forKey: "image")
actionSheet.addAction(action)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)

注意:IMP Line withRenderingMode(.alwaysOriginal)

Hardik Thakkar answered 2020-01-14T12:46:17Z
0 votes

迅捷版:

actionBtn.setValue(UIImage.init(named: "completeDose"), forKey: "image")
Dheeraj D answered 2020-01-14T12:46:36Z
-1 votes

迅捷的3扩展,属性和便利init。

extension UIAlertAction{
    @NSManaged var image : UIImage?

    convenience init(title: String?, style: UIAlertActionStyle,image : UIImage?, handler: ((UIAlertAction) -> Swift.Void)? = nil ){
        self.init(title: title, style: style, handler: handler)
        self.image = image
    }
}

感谢Shahar Stern的启发

Benny Davidovitz answered 2020-01-14T12:47:01Z
translate from https://stackoverflow.com:/questions/26347085/add-image-to-uialertaction-in-uialertcontroller