0

I would like to add Image in UIAlertbox so I add the following codes.

enter image description here

 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)

When I run the app, only one Image appear. What's wrong with this?
Please anyone help me?

May Phyu
  • 895
  • 3
  • 23
  • 47

1 Answers1

3

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)
seggy
  • 1,176
  • 2
  • 18
  • 38
  • Hello bro @seggy, You codes work!! Thanks a lot bro. But, I would like to edit the image size. So , I added this codes. let femaleimgView = UIImageView(frame: CGRect(x: 10, y: 10, width: 30, height: 30)) and femaleimgView.image = femaleimage. Action1.setValue(femaleimgView, forKey: "image"). When I add this codes, I got error. :( – May Phyu Mar 11 '17 at 03:32
  • is this useful for you? – seggy Mar 16 '17 at 07:57
  • Yes bro. Thanks :) – May Phyu Mar 16 '17 at 08:08
  • Hello @seggy, bro could you help me to view my link http://stackoverflow.com/questions/42830818/how-to-check-read-more-less-should-add-in-paragraph-with-swift-3 ? I have problem with this. – May Phyu Mar 16 '17 at 10:05
  • sorry dude i was buzy... are you using autolayout or autoresizing ? – seggy Mar 17 '17 at 11:24
  • It's ok bro @seggy. I encounter another problem. http://stackoverflow.com/questions/42942309/disable-alert-button-when-text-field-is-null-with-swift-3 . – May Phyu Mar 22 '17 at 03:41