将图像添加到 UIAlertController

Adding an image to a UIAlertController

这是我的警报,效果很好。我想向警报添加一个图像,当出现警报时,该图像会显示在文本旁边,如果它有所不同,我会在 SpriteKit 中的 SKScene 中。

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's 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)

您可以将 UIImageView 作为子视图添加到您的 UIAlertController

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

这就是你在 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)

你可以这样做。

    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)
    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

如果您愿意使用私有 APIs,您可以使用私有 attributedMessage 属性 将属性字符串设置为包含图像的消息:

来源: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")

同样,这是私有的 API,因此可能会在任何版本中更改,恕不另行通知。谨慎使用!

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)

注意:- 同样,这是私有的 API,因此可能会在任何版本中更改,恕不另行通知。谨慎使用!

谢谢

向 UIAlertController 添加图像

请尝试以下代码 Swift 4 +

  1. 添加 alertView 控制器

    let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.alert) 让图像 = UIImage(命名为:"feet")! alert.addImage(图片:图片) alert.addAction(UIAlertAction(标题:"OK",样式:UIAlertActionStyle.default,处理程序:无)) // 显示警报 self.present(警报,动画:真,完成:无)

  2. 添加扩展 UIAlertController

    func addImage(图像:UIImage){

    let maxSize = CGSize(width: 240, height: 244)
    let imgSize = image.size
    var ratio:CGFloat!
    if (imgSize.width > imgSize.height){
        ratio = maxSize.width / imgSize.width
    }else {
        ratio = maxSize.height / imgSize.height
    }
    let scaleSize = CGSize(width: imgSize.width*ratio, height: imgSize.height*ratio)
    var resizedImage = image.imageWithSize(scaleSize)
    
    if (imgSize.height > imgSize.width) {
        let left = (maxSize.width - resizedImage.size.width) / 2
        resizedImage = resizedImage.withAlignmentRectInsets(UIEdgeInsetsMake(0, -left, 0, 0))
    }
    
    let imgAction = UIAlertAction(title: "", style: .default, handler: nil)
    imgAction.isEnabled = false
    imgAction.setValue(resizedImage.withRenderingMode(.alwaysOriginal), forKey: "image")
    
    self.addAction(imgAction)
    

    }

添加图像视图扩展程序

扩展 UIImage {

func imageWithSize(_ size:CGSize) -> UIImage {
    var scaledImageRect = CGRect.zero

    let aspectWidth:CGFloat = size.width / self.size.width
    let aspectHeight:CGFloat = size.height / self.size.height
    let aspectRatio:CGFloat = min(aspectWidth, aspectHeight)

    scaledImageRect.size.width = self.size.width * aspectRatio
    scaledImageRect.size.height = self.size.height * aspectRatio
    scaledImageRect.origin.x = (size.width - scaledImageRect.size.width) / 2.0
    scaledImageRect.origin.y = (size.height - scaledImageRect.size.height) / 2.0

    UIGraphicsBeginImageContextWithOptions(size, false, 0)

    self.draw(in: scaledImageRect)

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return scaledImage!
}

}

目前,在不使用私有 api 或其他 hack 的情况下,没有真正正确的方法来使用 UIAlertController 执行此操作,因为根据文档,您不应修改警报视图控制器的视图层次结构。所以最好的解决方案是为此创建一个自定义视图。但是,如果您不关心一些额外的边框,那么在 UITextField 中使用属性字符串是可行的:

alertController.addTextField { field in
    let attachment = NSTextAttachment()
    attachment.image = image
    attachment.bounds = CGRect(x: 0, y: 0, width: 226, height: 226)
    field.attributedPlaceholder = NSAttributedString(attachment: attachment)
    field.textAlignment = .center
    field.isEnabled = false
}