4

Is there any way to add an images to action sheet on iOS ? Same as apple do on there app store or apple musics app. My basic search on apple docs indicated that I don't subclass or add sub view in Action Sheet.

"UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy." -- Apple Docs

enter image description here

Piyush Mathur
  • 1,617
  • 16
  • 33
AAV
  • 3,785
  • 8
  • 32
  • 59

5 Answers5

7

To add an image in an action sheet you need to do something like this...

Swift

let image = UIImage(named: "myImageName")
let action = UIAlertAction(title: "Action 1", style: .default, handler: nil);
action.setValue(image?.withRenderingMode(.alwaysOriginal), forKey: "image")

Objective C

UIAlertAction* action = [UIAlertAction actionWithTitle:@"Action 1" style:UIAlertActionStyleDefault handler:nil];
[action setValue:[[UIImage imageNamed:@"myImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];

Cheers :)

jeet.chanchawat
  • 5,842
  • 5
  • 38
  • 59
Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • Problem with this kind of solution is, if let's suppose apple change the name of the method from setImage "image" to setImg above code will start crashing. I know we can guard using #selector but still not a safe option. – AAV Dec 14 '17 at 18:13
  • 3
    How about the alignment to left? – Itachi Nov 15 '18 at 08:03
7

Swift 4: If the image is larger and it doesn't fit well, use this code and UIImage Extension to resize the image:

func showAlert() {
    let alertController = UIAlertController()
    let action = UIAlertAction(title: "Title", style: .default) { (action: UIAlertAction!) in
    }
    if let icon = icon?.imageWithSize(scaledToSize: CGSize(width: 32, height: 32)) {
        action.setValue(icon, forKey: "image")
    }

    alertController.addAction(action)
    self.present(alertController, animated: true, completion: nil)
}

And use this extension:

extension UIImage {

    func imageWithSize(scaledToSize newSize: CGSize) -> UIImage {

        UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
        self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return newImage
    }

}
Diego Carrera
  • 2,245
  • 1
  • 13
  • 16
2

In Swift It works only for UIAlertController Action Sheet

        let actionSheet = UIAlertController(title: "Action sheet style", message:"Action sheet with images", preferredStyle: .actionSheet)
        let action1 = addAction(UIAlertAction(title: "Action1", style: .default, handler: nil))
        let image1 = UIImage(named: "Action1.png")
        action1.setValue(image?.withRenderingMode(.alwaysOriginal), forKey: "image")
        actionSheet.addAction(action1)
        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(actionSheet, animated: true, completion: nil)
Sai kumar Reddy
  • 1,751
  • 20
  • 23
1

UIActionSheet is deprecated since iOS 8.3. Use UIAlertController and its UIAlertActions. Take a look at Add Image to UIAlertAction in UIAlertController

ryancrunchi
  • 465
  • 2
  • 16
0

UIAlertController has the same restriction that UIActionSheet had, there's no really good way to customize the layout.

You might want to check out libraries like SDCAlertView that offer functional equivalents along with better customization options.

Gereon
  • 17,258
  • 4
  • 42
  • 73
  • His solution works but doing something like this, looks wrong to me. var action = UIAlertAction(title: "Menu Item One", style: .default, handler: nil) action.setValue(image, forKey: "image") – AAV Dec 14 '17 at 16:39
  • I know and agree, and I'd never ship a hack like that. – Gereon Dec 14 '17 at 17:23