如何调整显示在警报控制器上的图像的大小?

时间:2019-02-15 09:20:05

标签: swift image swift3 uialertcontroller

我有一个按钮,每次按下该按钮都会显示一个UIAlertController,该UIAlertController会显示一个图像。我的问题是图像比Alertcontroller框大,我丢失了一部分。

如何调整图像大小以缩小图像?

我的Swift 3代码:

func buttonAction(sender: UIButton!) {

        let alertMessage = UIAlertController(title: "Alérgeno:", message: "", preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .default, handler: nil)

        action.setValue(UIImage(named: "zale1")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
        alertMessage .addAction(action)
        self.present(alertMessage, animated: true, completion: nil)

    }

这是结果

error AlertController

1 个答案:

答案 0 :(得分:1)

您正在使用 UIAlertViewController ,因此请定义一个图像视图并将该图像视图直接分配给uialertviewcontroller的视图

 @IBAction func buttonAction(sender: UIButton!) {

        let alertMessage = UIAlertController(title: "Alérgeno:", message: "", preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .default, handler: nil)
        let imageView = UIImageView(frame: CGRect(x: 10, y: 10, width: 40, height: 40))
       let image = UIImage(named: "your image name");
        imageView.image = image
        alertMessage.view.addSubview(imageView)

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

    }