Ios 将映像添加到UIAlertController
这是我的警觉,效果很好。我想在警报中添加一个图像,当警报出现时,它会显示在文本的旁边,如果它有影响,我会在SpriteKit中的一个场景中Ios 将映像添加到UIAlertController,ios,swift,uialertcontroller,Ios,Swift,Uialertcontroller,这是我的警觉,效果很好。我想在警报中添加一个图像,当警报出现时,它会显示在文本的旁边,如果它有影响,我会在SpriteKit中的一个场景中 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 wh
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)
Swift 3如果您愿意使用私有API,可以使用私有
attributedMessage
属性将属性字符串设置为包含图像的消息:
资料来源:
let actionSheet=UIAlertController(标题:“title”,消息:nil,首选样式:。actionSheet)
让str=NSMutableAttributedString(字符串:“消息\n\n”,属性:[NSAttributedStringKey.font:UIFont.preferredFont(forTextStyle:.caption1),NSAttributedStringKey.foregroundColor:UIColor.gray])
let attachment=NSTextAttachment()
附件.image=-->yourImage SWIFT 4+
注意:-同样,这是一个私有API,因此可能会在任何版本中更改,恕不另行通知。小心使用
感谢向UIAlertController添加图像
请尝试下面的代码Swift 4+
添加alertView控制器
let alert=UIAlertController(标题:title,消息:msg,首选样式:UIAlertControllerStyle.alert)
让image=UIImage(名为“脚”)!
警报。添加图像(图像:图像)
addAction(UIAlertAction(标题:“确定”,样式:UIAlertActionStyle.default,处理程序:nil))
//显示警报
self.present(警报、动画:true、完成:nil)
添加扩展UIAlertController
func addImage(图像:UIImage){
}
添加图像视图扩展
扩展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)
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或其他黑客的情况下,使用UIAlertController确实没有一种正确的方法,因为根据文档,您不应该修改alert view controller的视图层次结构。因此,最好的解决方案是为此创建自定义视图。但是,如果您不关心一些额外的边界,那么在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
}
这些方法是不兼容的。它有效地将图像添加到按钮区域,并影响警报视图中其他按钮的大小。这种方法的可访问性也可能不兼容。苹果公司本应该能够做到这一点,但如果没有这一选项,从UIView
生成的定制警报似乎是最好的方法,并且具有控制所有元素的能力。
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)
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!
}
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
}