将图像添加到警报视图
问题描述:
我有一个警报视图,当用户按下添加按钮时会弹出。如何将图像添加到警报视图?
I have an alert view that pops up when the user press the add button. How do i add an image to the alert view?
我添加了一些我从堆栈溢出引用的代码。我的保存按钮被替换为图像,图像显示为蓝色...
I added some code that i took reference from stack overflow. My save button is replaced with the image and the image appear to be in blue colour...
警报视图代码
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)
答
是的,您可以将 UIImageView
作为子视图添加到您的提醒视图。
Yes, you can add a UIImageView
as a subview to your alertview.
var imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40))
imageView.image = yourImage
alert.view.addSubview(imageView)