在swift中将图像添加到UIAlertController中

时间:2014-12-20 09:30:42

标签: image swift uialertcontroller

我想要一个带有图像的简单UIAlertController。我该怎么办?

let alertMessage = UIAlertController(title: "Service Unavailable", message: "Please retry later", preferredStyle: .Alert)
                alertMessage.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
                self.presentViewController(alertMessage, animated: true, completion: nil)

4 个答案:

答案 0 :(得分:11)

试试这个:

let alertMessage = UIAlertController(title: "Service Unavailable", message: "Please retry later", 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)

答案 1 :(得分:2)

我不认为可以将图像添加到UIAlertController。

答案 2 :(得分:0)

一段适合我的代码:

func alert2 (heading: String,image: String){
        if self.presentedViewController == nil {
            let alertController = UIAlertController(title: nil,     message: " ", preferredStyle: .Alert )
            var imageView = UIImageView(frame: CGRectMake(10, 6, 300, 50))
            imageView.image = UIImage(named: display.0)
            alertController.view.addSubview(imageView)
            alertController.addAction(UIAlertAction(title: "Maps", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction!) in self.makeContact("maps")}))

答案 3 :(得分:-1)

这是我在警报视图中成像的代码

var logoAlert = UIImageView(frame: CGRectMake(0, 0, 300, 250))
    logoAlert.image = UIImage(named: "v1.png")

    infoController.view.addSubview(logoAlert)
    NSLog("image is draw")

logoalert是dtring infoController - 有趣的名字 重命名,一切都会好起来的 将此代码粘贴到警报控制器

的乐趣中