如何在 Swift 3 中将图像添加到 UIAlertbox? [英] How Add Image into UIAlertbox in Swift 3?

查看:28
本文介绍了如何在 Swift 3 中将图像添加到 UIAlertbox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 UIAlertbox 中添加图片,所以我添加了以下代码.

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

 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?

推荐答案

试试这段代码在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

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

pod 'FontAwesome.swift' 并导入 FontAwesome_swift

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)

这篇关于如何在 Swift 3 中将图像添加到 UIAlertbox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆