Xcode Swift How to Add Image to Uialertcontroller Options

Xcode Swift how to add image to UIAlertController options?


 let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)            
alert.modalPresentationStyle = .popover


let image = UIImage(named: "logoCircle")
let imageView = UIImageView()
imageView.image = image
imageView.frame = CGRect(x: 25, y: 18, width: 24, height: 24)
alert.view.addSubview(imageView)

let image1 = UIImage(named: "icshare")
let imageView1 = UIImageView()
imageView1.image = image1
alert.view.addSubview(imageView1)
imageView1.frame = CGRect(x: 25, y: 75, width: 24, height: 24)

let shareExternal = UIAlertAction(title: NSLocalizedString("Share External Link", comment: ""), style: .default) { action in
}
let shareInApp = UIAlertAction(title: "Share within", style: .default) {
action in
}

alert.addAction(shareInApp)
alert.addAction(shareExternal)


if let presenter = alert.popoverPresentationController
{
presenter.sourceView = button
presenter.sourceRect = button.bounds
}
present(alert, animated: true, completion: nil)

Adding an image to a UIAlertController

You can add a UIImageView as a subview to your UIAlertController.

var imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40))
imageView.image = yourImage
alert.view.addSubview(imageView)

This is how you do in UIAlertController:

let alertMessage = UIAlertController(title: "My Title", message: "My Message", preferredStyle: .Alert)

let image = UIImage(named: "myImage")
var action = UIAlertAction(title: "OK", style: .Default, handler: nil)
action.setValue(image, forKey: "image")
alertMessage .addAction(action)

self.presentViewController(alertMessage, animated: true, completion: nil)

Swift - UIAlertController with image

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)

Add image into UIAlertController in swift

Try this:

let alertMessage = UIAlertController(title: "Service Unavailable", message: "Please retry later", preferredStyle: .Alert)

let image = UIImage(named: "myImage")
var action = UIAlertAction(title: "OK", style: .Default, handler: nil)
action.setValue(image, forKey: "image")
alertMessage .addAction(action)

self.presentViewController(alertMessage, animated: true, completion: nil)

Add Image in Alert Message box Title with Swift 3

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)

How to add UIImageView in UIAlertController?

It is not possible to add an image to a UIAlertController according to Apple Doc.

if you want then you can create your own custom view like:

https://github.com/wimagguc/ios-custom-alertview

If you want to take image appear on button
then Try like this:

UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Title"
message:@"Welcome"
preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* okButton = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
//Do some thing here
}];

[okButton setValue:[[UIImage imageNamed:@"kaga.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];

[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];


Related Topics



Leave a reply



Submit