3

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)
Karthikeyan.R.S
  • 3,971
  • 1
  • 18
  • 31
daaniaal
  • 491
  • 7
  • 12

4 Answers4

10

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)
Mrunal
  • 13,672
  • 6
  • 49
  • 93
  • 1
    This effectively adds an image to the button area of an alert view. It's not the correct approach. Affects the button size and likely lacks accessibility. A custom alert is more effective in the absence of simple method provided by Apple. – user4806509 Aug 05 '17 at 22:09
1

I don't think it is possible to add an image to a UIAlertController.

Jojodmo
  • 23,136
  • 13
  • 61
  • 102
Dharmesh Kheni
  • 69,532
  • 33
  • 158
  • 163
0

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")}))
Jeremy
  • 145
  • 1
  • 2
  • 12
-1

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

Mobu Duon
  • 11
  • 1
  • Welcome to StackOverflow! If you edit your answer to contain a readable, English description (instead of "dtring", "fun"), it will be easier to understand and increase the chance of people voting for it. :-) – Michael Jaros Mar 09 '15 at 11:45
  • I don't know what you had before to get the down votes, but you fixed it and your code worked for me – David Sanford Jul 17 '17 at 07:15