如何在 Swift 3 中将图像添加到 UIAlertbox?

How Add Image into UIAlertbox in Swift 3?

我想在 UIAlertbox 中添加图像,所以我添加了以下代码。

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

    // Create the actions
       let okAction = UIAlertAction(title: "Female", style: UIAlertActionStyle.default) {
            UIAlertAction in
                // exit(0)
                debugPrint("Press OK")

       }
       let cancelAction = UIAlertAction(title: "Male", style: UIAlertActionStyle.cancel) {
                UIAlertAction in

       }


// Add the actions
   okAction.setValue(#imageLiteral(resourceName: "female_image"), forKey: "image")
   cancelAction.setValue(#imageLiteral(resourceName: "male_image"), forKey: "image")
   alertController.addAction(okAction)
   alertController.addAction(cancelAction)

当我运行应用程序时,只出现一张图片。这有什么问题吗?
请任何人帮助我?

尝试使用此代码在 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)

快乐编码:-)

要更改图像大小,您可以尝试使用 fontAwesome,只需安装 pod

pod 'FontAwesome.swift' and import FontAwesome_swift

这是代码.....

    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)