【问题标题】:How Add Image into UIAlertbox in Swift 3?如何在 Swift 3 中将图像添加到 UIAlertbox?
【发布时间】:2017-03-10 09:46:00
【问题描述】:

我想在 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 uiimageview uialertview uialertcontroller


    【解决方案1】:

    试试这段代码,它在 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' 并导入 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)
    

    【讨论】:

    • 你好兄弟@seggy,你的代码工作!非常感谢兄弟。但是,我想编辑图像大小。所以,我添加了这个代码。让 femaleimgView = UIImageView(frame: CGRect(x: 10, y: 10, width: 30, height: 30)) 和 femaleimgView.image = femaleimage. Action1.setValue(femaleimgView, forKey: "image")。添加此代码时,出现错误。 :(
    • 这对你有用吗?
    • 是的,兄弟。谢谢:)
    • 你好@seggy,兄弟你能帮我查看我的链接stackoverflow.com/questions/42830818/…吗?我有这个问题。
    • 对不起,我很忙...您使用的是自动布局还是自动调整大小?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多