Add image into UIAlertController in swift

末鹿安然 提交于 2019-12-10 10:07:11

问题


I want to have a simple UIAlertController with image in it. what should I do?

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)

回答1:


Try this:

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)



回答2:


I don't think it is possible to add an image to a UIAlertController.




回答3:


a snippet of code that works for me:

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



回答4:


this is my code to image in alert view

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

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

logoalert is dtring infoController - is name of fun rename it, and all will be alright paste this code in fun of alert controller



来源:https://stackoverflow.com/questions/27578866/add-image-into-uialertcontroller-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!