Add Image to UIAlertAction in UIAlertController

user1079052 picture user1079052 · Oct 13, 2014 · Viewed 54k times · Source

I have seen a couple screen shots of a UIAlertControllers with an image on the left of the row but I do not seen it in the documentation. An example visual is http://imgur.com/SISBvjB Here is the code that I have for my controller right now:

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

Answer

JP Hribovsek picture JP Hribovsek · Oct 14, 2014

And that's how it's done:

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

the image property is not exposed, so there's no guarantee of this working in future releases, but works fine as of now