Add image to UIAlertController in swift
I want to have a simple UIAlertController with an 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)
+3
source to share
4 answers
Try the following:
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)
+11
source to share
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")}))
0
source to share
this is my code for image in alert mode
var logoAlert = UIImageView(frame: CGRectMake(0, 0, 300, 250))
logoAlert.image = UIImage(named: "v1.png")
infoController.view.addSubview(logoAlert)
NSLog("image is draw")
logoalert - dtring infoController is the name is fun rename it and you will be fine paste this code into the fun of the alert controller
-1
source to share