Add Image to UIAlertAction in UIAlertController Add Image to UIAlertAction in UIAlertController ios ios

Add Image to UIAlertAction in UIAlertController


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


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


You could add an image above the title label by subclassing UIAlertController and adding \n to the title string to make space for the UIImageView. You'd have to compute the layout based on the font size. For images in the UIAlertAction use KVC like so self.setValue(image, forKey: "image"). I would recommend to use an extension that checks for responds(to:). Here is sample implementation.

enter image description here