How Add Image into UIAlertbox in Swift 3?

我只是一个虾纸丫 提交于 2019-12-02 08:10:10

Try this code its working in swift3

    let alertMessage = UIAlertController(title: "Gender", message: "", preferredStyle: .alert)

    let image = UIImage(named: "blanckstar")
    let action = UIAlertAction(title: "Male", style: .default)
    {
        UIAlertAction in
        // exit(0)
        debugPrint("Press Male")

    }
    action.setValue(image, forKey: "image")

    let image2 = UIImage(named: "blanckstar")
    let action2 = UIAlertAction(title: "Female", style: .default)
    {
        UIAlertAction in
        // exit(0)
        debugPrint("Press Female")

    }

    action2.setValue(image2, forKey: "image")

    alertMessage.addAction(action)
    alertMessage.addAction(action2)

    self.present(alertMessage, animated: true, completion: nil)

Happy Coading :-)

for changing size of image you can try with fontAwesome just install pod

pod 'FontAwesome.swift' and import FontAwesome_swift

here is the code.....

    let alertMessage = UIAlertController(title: "Gender", message: "", preferredStyle: .alert)

    let image = UIImage.fontAwesomeIcon(name: .male, textColor: UIColor.black, size: CGSize(width: 50, height: 50))
    let action = UIAlertAction(title: "Male", style: .default)
    {
        UIAlertAction in
        // exit(0)
        debugPrint("Press Male")

    }
    action.setValue(image, forKey: "image")

    let image2 = UIImage.fontAwesomeIcon(name: .female, textColor: UIColor.black, size: CGSize(width: 50, height: 50))
    let action2 = UIAlertAction(title: "Female", style: .default)
    {
        UIAlertAction in
        // exit(0)
        debugPrint("Press Female")

    }

    action2.setValue(image2, forKey: "image")

    alertMessage.addAction(action)
    alertMessage.addAction(action2)

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