Подтвердить что ты не робот

Добавить изображение в режим предупреждения

У меня появляется предупреждение, которое появляется, когда пользователь нажимает кнопку добавления. Как добавить изображение в представление предупреждения?

Я добавил код, который я использовал для. Моя кнопка сохранения заменена изображением, и изображение выглядит синим цветом...

Код для просмотра предупреждений

var alert = UIAlertController(title: "Spring Element \(springNumber)",
            message: "Add spring properties",
            preferredStyle: .Alert)

        let saveAction = UIAlertAction(title: "Save",
            style: .Default) { (action: UIAlertAction!) -> Void in

                let textField1 = alert.textFields![0] as UITextField
                self.txtField1.append(textField1.text)
                self.tableView.reloadData()

                let textField2 = alert.textFields![1] as UITextField
                self.txtField2.append(textField2.text)
                self.tableView.reloadData()

                println(self.txtField1)
                println(self.txtField2)
        }

        let cancelAction = UIAlertAction(title: "Cancel",
            style: .Default) { (action: UIAlertAction!) -> Void in
        }

        //adding textfield1
        alert.addTextFieldWithConfigurationHandler {
            (textField1: UITextField!) -> Void in
            textField1.placeholder = "Force"
        }

        //adding textfield2
        alert.addTextFieldWithConfigurationHandler {
            (textField2: UITextField!) -> Void in
            textField2.placeholder = "Stiffness"
        }

        alert.addAction(saveAction)
        alert.addAction(cancelAction)

        presentViewController(alert,
            animated: true,
            completion: nil)

Код для изображения

   let image = UIImage(named: "springAtWall")
    saveAction.setValue(image, forKey: "image")
    alert.addAction(saveAction)
4b9b3361

Ответ 1

Да, вы можете добавить UIImageView в качестве подзаголовка к вашему предупреждению.

var imageView = UIImageView(frame: CGRect(x: 220, y: 10, width: 40, height: 40))
imageView.image = yourImage

alert.view.addSubview(imageView)

Ответ 2

Swift 4:

var imageView = UIImageView(frame: CGRect(x: 220, y: 10, width: 40, height: 40))
imageView.image = <#yourImage#>
alert.view.addSubview(imageView)

Ответ 3

Вот решение для Swift 4:

let showAlert = UIAlertController(title: "Demo Alert", message: nil, preferredStyle: .alert)
let imageView = UIImageView(frame: CGRect(x: 10, y: 50, width: 250, height: 230))
imageView.image = image // Your image here...
showAlert.view.addSubview(imageView)
let height = NSLayoutConstraint(item: showQRCodeAlert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 320)
let width = NSLayoutConstraint(item: showQRCodeAlert.view, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 250)
showAlert.view.addConstraint(height)
showAlert.view.addConstraint(width)
showAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
    // your actions here...    
}))
self.present(showAlert, animated: true, completion: nil)

Выход будет как-то ниже для всех iPhone:

Alert with Image

Ответ 4

Мы можем добавить изображение как один из параметров в диспетчере виде предупреждений, как это.

   let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 196, height: 196)))
    imageView.image = image

    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, imageView.isOpaque, 0.0)
    defer { UIGraphicsEndImageContext() }
    let context = UIGraphicsGetCurrentContext()
    imageView.layer.render(in: context!)
    let finalImage = UIGraphicsGetImageFromCurrentImageContext()

    let alertMessage = UIAlertController(title: "Your Title", message: "", preferredStyle: .alert)
    let action = UIAlertAction(title: "", style: .default, handler: nil)
    action.setValue(finalImage?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
    alertMessage .addAction(action)
    let action1 = UIAlertAction(title: "OK", style: .default, handler: nil)
    alertMessage .addAction(action1)

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

Ответ 5

К сожалению, у меня нет представителя, чтобы комментировать один из предыдущих ответов, но у меня есть вопрос к ответу @Hardicks.

Для NSLayoutConstraint действительно ли showQRCodeAlert представляет Viewcontroller?