2

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))
Hamish
  • 78,605
  • 19
  • 187
  • 280
May Phyu
  • 895
  • 3
  • 23
  • 47
  • have you got an answer of your question or not? – Hardik Shekhat Mar 02 '17 at 13:17
  • I haven't try it yet bro @HardikShekhat. I will try it asap. I'm not well within these days. that's why. Thanks for asking me. :) I will reply the result. – May Phyu Mar 03 '17 at 03:56
  • Hello bro @HardikShekhat, I try your code now. Image appears within the alert box but I would like to show it in alert box title. Currently, I remove title text. So, how can I put that image in title place bro? – May Phyu Mar 03 '17 at 04:19
  • 1
    Bro @HardikShekhat, it is ok now. let alertController = UIAlertController(title:"", message:"\n I would like to share.", preferredStyle: .alert) and I adjust x, y value. Now, it is fine now bro. Thanks a lot. :) – May Phyu Mar 03 '17 at 04:52
  • I have tried below code before write answer. And it appears image in Title place. Maybe you have written something wrong? so, please check it again. – Hardik Shekhat Mar 03 '17 at 04:54
  • According to your code, Image appears in top left of the alert box. – May Phyu Mar 03 '17 at 05:05
  • you are right. I have put image in top left corner. you can edit code of my answer with your code if you want. – Hardik Shekhat Mar 03 '17 at 05:15
  • Hello bro @HardikShekhat, please could you look at this link http://stackoverflow.com/questions/42573338/swrevealviewcontroller-button-not-work-after-clicking-back-button-from-another-v . No one answer my question. I have to finish my project urgently. I know I should not write like this in here. But please help me if you know bro. – May Phyu Mar 03 '17 at 07:59

2 Answers2

7

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)
Hardik Shekhat
  • 1,680
  • 12
  • 21
  • Hi bro @HardikShekhat bro , could you please check this link? http://stackoverflow.com/questions/42715152/add-image-into-uialertbox-in-swift-3 I also encountered another alert message problem :( – May Phyu Mar 10 '17 at 09:52
0

Here is an extension for UIAlertController that allows to add image above the title.

Ps. I use snapkit for constraints, you may want to update them with your preferred way of doing layouts.

extension UIAlertController {
    
    func configure(with image: UIImage, title: String, handler: Action?) {
        self.title = ""

        addAction(UIAlertAction(
            title: Localizable.Shared.cancel,
            style: .default
        ))
        addAction(UIAlertAction(
            title: Localizable.Shared.yes,
            style: .destructive,
            handler: { [weak self] _ in
                handler?()
                self?.dismiss(animated: true)
            }
        ))
        
        let imageView = UIImageView()
        imageView.image = image
        
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.textAlignment = .center
        //Configure font, color etc
        
        view.addSubview(imageView)
        view.addSubview(titleLabel)
        
        imageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().inset(20)
            make.width.height.equalTo(125)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(imageView.snp.bottom).inset(16)
        }
    }
}
Shalugin
  • 1,092
  • 2
  • 10
  • 15