swiftuialertviewuialertcontrolleruialertaction

Add Image in Alert Message box Title with Swift 3


I would like to add Share Image in alert message title instead of title text.
How can I implement this with swift 3?

let alertController = UIAlertController(title: "Share Movie", message: "Share this movie!", preferredStyle: .alert)

    alertController.addAction(UIAlertAction(title: "Share", style: UIAlertActionStyle.default, handler: nil))

Solution

  • Try this. Maybe help you.

    let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
    
    let action = UIAlertAction(title: "OK", style: .default, handler: nil)
    
    let imgTitle = UIImage(named:"imgTitle.png")
    let imgViewTitle = UIImageView(frame: CGRect(x: 10, y: 10, width: 30, height: 30))
    imgViewTitle.image = imgTitle
    
    alert.view.addSubview(imgViewTitle)
    alert.addAction(action)
    
    self.present(alert, animated: true, completion: nil)