daaniaal
daaniaal

Reputation: 501

Add image into UIAlertController in swift

I want to have a simple UIAlertController with image in it. what should I do?

let alertMessage = UIAlertController(title: "Service Unavailable", message: "Please retry later", preferredStyle: .Alert)
                alertMessage.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
                self.presentViewController(alertMessage, animated: true, completion: nil)

Upvotes: 3

Views: 9480

Answers (3)

Jeremy
Jeremy

Reputation: 145

a snippet of code that works for me:

func alert2 (heading: String,image: String){
        if self.presentedViewController == nil {
            let alertController = UIAlertController(title: nil,     message: " ", preferredStyle: .Alert )
            var imageView = UIImageView(frame: CGRectMake(10, 6, 300, 50))
            imageView.image = UIImage(named: display.0)
            alertController.view.addSubview(imageView)
            alertController.addAction(UIAlertAction(title: "Maps", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction!) in self.makeContact("maps")}))

Upvotes: 0

Mobu Duon
Mobu Duon

Reputation: 11

this is my code to image in alert view

var logoAlert = UIImageView(frame: CGRectMake(0, 0, 300, 250))
    logoAlert.image = UIImage(named: "v1.png")

    infoController.view.addSubview(logoAlert)
    NSLog("image is draw")

logoalert is dtring infoController - is name of fun rename it, and all will be alright paste this code in fun of alert controller

Upvotes: -1

Mrunal
Mrunal

Reputation: 14118

Try this:

let alertMessage = UIAlertController(title: "Service Unavailable", message: "Please retry later", preferredStyle: .Alert)

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

self.presentViewController(alertMessage, animated: true, completion: nil)

Upvotes: 10

Related Questions