Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Image to UIAlertAction in UIAlertController

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]; 
like image 227
user1079052 Avatar asked Oct 13 '14 19:10

user1079052


2 Answers

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

like image 153
JP Hribovsek Avatar answered Sep 30 '22 13:09

JP Hribovsek


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]; 
like image 22
dheerendra Avatar answered Sep 30 '22 13:09

dheerendra