Adding an Image to the UIAlertController

This is my warning that works fine. I want to add an image to the alert that will be displayed along the text when the alert is presented, and I'm in SKScene in SpriteKit, if that matters.

var alertController = UIAlertController(title: "How to Skate", message: "Tap the screen to perform a trick and jump over the obsticles (You can grind on rails) The game will end when you hit a highlighted red, orange or yellow obstacle. That it! + Image", preferredStyle: UIAlertControllerStyle.Alert) alertController.addAction(UIAlertAction(title: "Cool!", style: UIAlertActionStyle.Cancel, handler: nil)) self.view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil) 
+8
source share
6 answers

You can add a UIImageView as a subtitle to the UIAlertController .

 var imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40)) imageView.image = yourImage alert.view.addSubview(imageView) 

Here is how you do it in UIAlertController :

 let alertMessage = UIAlertController(title: "My Title", message: "My Message", 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) 
+8
source
  let alertMessage = UIAlertController(title: "My Title", message: "", preferredStyle: .alert) let action = UIAlertAction(title: "OK", style: .default, handler: nil) action.setValue(UIImage(named: "task1.png")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image") alertMessage .addAction(action) self.present(alertMessage, animated: true, completion: nil) 

Swift 3

+4
source

You can do it.

  let imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40)) // imageView.image = UIImage(named: "ic_no_data") let alertMessage = UIAlertController(title: "My Title", message: "", preferredStyle: .Alert) let image = UIImage(named: "Image") let action = UIAlertAction(title: "OK", style: .Default, handler: nil) action.setValue(image, forKey: "image") alertMessage .addAction(action) self.presentViewController(alertMessage, animated: true, completion: nil) alertMessage.view.addSubview(imageView) 
+3
source

If you want to use private APIs, you can use the attributedMessage private property to set the attributed string as a message containing image:

Source: https://github.com/JaviSoto/iOS10-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UIAlertController.h

 let actionSheet = UIAlertController(title: "title", message: nil, preferredStyle: .actionSheet) let str = NSMutableAttributedString(string: "Message\n\n", attributes: [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: .caption1), NSAttributedStringKey.foregroundColor: UIColor.gray]) let attachment = NSTextAttachment() attachment.image = --> yourImage <-- str.append(NSAttributedString(attachment: attachment)) actionSheet.setValue(str, forKey: "_attributedMessage") 

Again, this is a private API, and therefore it is subject to change in any release without notice. Use with caution!

0
source

SWIFT 4+

  let refreshAlert = UIAlertController(title: "Unlike Article", message: "Are you sure you want to unlike this Article?", preferredStyle: UIAlertControllerStyle.alert) let cancel = UIAlertAction(title: "CANCEL", style: .default, handler: { (action: UIAlertAction!) in return }) let image = #imageLiteral(resourceName: "dislike_icon").resizedImage(newSize: CGSize(width: 25, height: 25)) let unLike = UIAlertAction(title: "UNLIKE", style: .destructive, handler: { (action: UIAlertAction!) in self.articleUnLiking() return }) unLike.setValue(image.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image") refreshAlert.addAction(cancel) refreshAlert.addAction(unLike) self.present(refreshAlert, animated: true, completion: nil) 

NOTE. - Again, this is a private API, and therefore it may change in any release without notice. Use with caution!

THANKS

0
source

I highly recommend that you use this library to create a user dialog is very simple:

https://github.com/vikmeup/SCLAlertView-Swift

You just need to create an image representation programmatically and add it to the preview:

 let imageView = UIIMageView(frame: CGRectMake(x,10,180,25)) imageView.image = UIImage(named:"image.jpg") imageView.layer.borderColor = UIColor.greenColor().CGColor subview.addSubview(imageView) 

Hope this helps sleep!

-1
source

All Articles