Search code examples
iosswiftswift3messageboxuialertcontroller

Add image in the message box (UIAlertController)


I'm using UIAlertController() how can I add just title and image in the my message box like this:

enter image description here


Solution

  • Swift 3.0

    Use below code

    let alertMessage = UIAlertController(title: "My Title", message: "\n\n\n\n\n\n\n\n\n\n\n\n", preferredStyle: .alert)
    let action = UIAlertAction(title: "OK", style: .default, handler: nil)
    alertMessage .addAction(action)
    self.present(alertMessage, animated: true, completion: nil)
    
    let xPosition = self.view.frame.origin.x + 80
    let imageView = UIImageView(frame: CGRect(x: xPosition, y: 100, width: 100, height: 100))
    imageView.image = #imageLiteral(resourceName: "test.png")
    alertMessage.view.addSubview(imageView)
    

    Without "OK" button you cannot to close above alertMessage.
    Set xPosition and y of imageViewand message of alertMessage as per your requirement.