当用户按下添加按钮时,我会弹出一个警报视图。如何将图像添加到警报视图?
我添加了一些我从堆栈溢出引用的代码。我的保存按钮被图像替换,图像显示为蓝色......
警报视图代码
var alert = UIAlertController(title: "Spring Element \(springNumber)",
message: "Add spring properties",
preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save",
style: .Default) { (action: UIAlertAction!) -> Void in
let textField1 = alert.textFields![0] as UITextField
self.txtField1.append(textField1.text)
self.tableView.reloadData()
let textField2 = alert.textFields![1] as UITextField
self.txtField2.append(textField2.text)
self.tableView.reloadData()
println(self.txtField1)
println(self.txtField2)
}
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction!) -> Void in
}
//adding textfield1
alert.addTextFieldWithConfigurationHandler {
(textField1: UITextField!) -> Void in
textField1.placeholder = "Force"
}
//adding textfield2
alert.addTextFieldWithConfigurationHandler {
(textField2: UITextField!) -> Void in
textField2.placeholder = "Stiffness"
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert,
animated: true,
completion: nil)
图片视图代码
let image = UIImage(named: "springAtWall")
saveAction.setValue(image, forKey: "image")
alert.addAction(saveAction)
答案 0 :(得分:26)
是的,您可以将UIImageView
作为子视图添加到警报视图中。
var imageView = UIImageView(frame: CGRect(x: 220, y: 10, width: 40, height: 40))
imageView.image = yourImage
alert.view.addSubview(imageView)
答案 1 :(得分:5)
var imageView = UIImageView(frame: CGRect(x: 220, y: 10, width: 40, height: 40))
imageView.image = <#yourImage#>
alert.view.addSubview(imageView)
答案 2 :(得分:5)
这是Swift 4的解决方案:
let showAlert = UIAlertController(title: "Demo Alert", message: nil, preferredStyle: .alert)
let imageView = UIImageView(frame: CGRect(x: 10, y: 50, width: 250, height: 230))
imageView.image = image // Your image here...
showAlert.view.addSubview(imageView)
let height = NSLayoutConstraint(item: showAlert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 320)
let width = NSLayoutConstraint(item: showAlert.view, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 250)
showAlert.view.addConstraint(height)
showAlert.view.addConstraint(width)
showAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
// your actions here...
}))
self.present(showAlert, animated: true, completion: nil)
答案 3 :(得分:2)
我们可以像这样在警报视图控制器中添加图像作为一个选项。
let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 196, height: 196)))
imageView.image = image
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, imageView.isOpaque, 0.0)
defer { UIGraphicsEndImageContext() }
let context = UIGraphicsGetCurrentContext()
imageView.layer.render(in: context!)
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
let alertMessage = UIAlertController(title: "Your Title", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "", style: .default, handler: nil)
action.setValue(finalImage?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
alertMessage .addAction(action)
let action1 = UIAlertAction(title: "OK", style: .default, handler: nil)
alertMessage .addAction(action1)
self.present(alertMessage, animated: true, completion: nil)
答案 4 :(得分:0)
我现在还不清楚@Kakumanu的ImageContext的用途是什么,但这是为了调整UIImage的大小。也许稍微快一点的方法是通过UIImage扩展```
extension UIImage {
/// Resize a UIImage
func imageWith(newSize: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: newSize)
let image = renderer.image { _ in
self.draw(in: CGRect.init(origin: CGPoint.zero, size: newSize))
}
return image.withRenderingMode(self.renderingMode)
}
}