Add image to UIAlertController [closed]
Asked Answered
J

1

3

I want to add an image to UIAlertController. Image does not need to be on a button, just presented in the middle of the controller. The code I have is below but crashes with message "unrecognised selector sent to instance".

func showAlert () {

    let alert = UIAlertController(title: "Title of Alert",
        message: "none",
        preferredStyle:UIAlertControllerStyle.ActionSheet)

    // add an image
    let image = UIImage(named: "example")
    var imageView = UIImageView(image: image)
    imageView.frame = CGRectMake(0, 0, 100, 100)

    var imageAction = UIAlertAction(title: "", style:.Default, handler: nil)
    imageAction.setValue(imageView, forKey: "image")
    alert.addAction(imageAction)

    // add a continue button
    var action = UIAlertAction(title: "Continue", style:.Default, handler: {(alert:UIAlertAction!) in self.continue() })
    alert.addAction(action)

    // show the UIAlertController
    self.presentViewController(alert, animated: true, completion: nil)

}
Jerilynjeritah answered 17/8, 2015 at 20:40 Comment(1)
What is the unrecognized selector?Stockbreeder
F
4

Unfortunately Apple does not allow us to add images to UIAlertController or UIAlertView. These APIs are limited to Apple's Human Interface Guidelines.

If you really need an image in a UIAlertController-like dialog, I would recommend trying to use something like https://github.com/wimagguc/ios-custom-alertview.

Forced answered 17/8, 2015 at 21:5 Comment(2)
well if its not possible, its not possible! Will have to use a View Controller instead then....Jerilynjeritah
There is a way to do it though stringcode.co.uk/alertviewcontrollerNeisa

© 2022 - 2024 — McMap. All rights reserved.