Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - UIAlertController with image

Tags:

ios

swift

iphone

I have this code:

let alert = UIAlertController(title:"title", preferredStyle: UIAlertControllerStyle.alert)
    let saveAction = UIAlertAction(title: "Title", style: .default, handler: nil)

    var imageView = UIImageView(frame: CGRectMake(10, 10, 250, 124))
    imageView.image = zdjecieGlowne
    alert.view.addSubview(imageView)

    alert.addAction(UIAlertAction(title: "Button 1", style: UIAlertActionStyle.default, handler: { alertAction in
        print("1 pressed")
    }))
    alert.addAction(UIAlertAction(title: "Button 2", style: UIAlertActionStyle.cancel, handler: { alertAction in
        print("2 pressed")
    }))
    alert.addAction(UIAlertAction(title: "Button 3", style: UIAlertActionStyle.default, handler: { alertAction in
        print("3 pressed")
    }))
    self.present(alert, animated: true, completion: nil)
}

The window looks like this: https://ibb.co/hM5zHH

The picture covers me with the buttons. Does anyone know how to fix it?

like image 899
Łukasz Betta Avatar asked Aug 17 '26 11:08

Łukasz Betta


2 Answers

Try this, it may helps you

    let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
    let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)

    //Add imageview to alert
    let imgViewTitle = UIImageView(frame: CGRect(x: 10, y: 10, width: 30, height: 30))
    imgViewTitle.image = UIImage(named:"image.png")
    alert.view.addSubview(imgViewTitle)

    alert.addAction(action)
    self.present(alert, animated: true, completion: nil)
like image 110
Rohit Makwana Avatar answered Aug 20 '26 00:08

Rohit Makwana


In my opinion UIAlertController shouldn't be used to show some option with image, instead you should implement custom UIView and handle presentation logic.

like image 34
mkowal87 Avatar answered Aug 19 '26 23:08

mkowal87



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!