Add image to alert view

后端 未结 5 1785
鱼传尺愫
鱼传尺愫 2021-02-02 12:08

I have an alert view that pops up when the user press the add button. How do i add an image to the alert view?

I added some code that i took reference from stack overfl

5条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 12:29

    Here is the solution for 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: showAlert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 320)
    let width = NSLayoutConstraint(item: showAlert.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)
    

    Output will be somehow like below for all iPhones:

提交回复
热议问题